Php 如何以字符串形式获取节点的xml内容?
使用PHP的DOM函数,如何将DOM节点的内容转换为字符串Php 如何以字符串形式获取节点的xml内容?,php,xml,dom,Php,Xml,Dom,使用PHP的DOM函数,如何将DOM节点的内容转换为字符串 <foo> Blah blah <bar baz="1">bah</bar> blah blah </foo> 废话废话 给定foo作为当前上下文节点,如何将'Blah Blah bah Blah Blah'作为字符串?使用$node->textContent或$node->nodeValue只返回文本节点,而不是位 基本上,Javascript的innerHTML属性的等价
<foo>
Blah blah <bar baz="1">bah</bar> blah blah
</foo>
废话废话
给定foo
作为当前上下文节点,如何将'Blah Blah bah Blah Blah'
作为字符串?使用$node->textContent
或$node->nodeValue
只返回文本节点,而不是
位
基本上,Javascript的
innerHTML
属性的等价物…您可以从
节点创建一个新的DOMDocument,并将其解析为XML或HTML:
function getInnerHTML($node) {
$tmpDoc = new DOMDocument('1.0');
$block = $tmpDoc->importNode($node->cloneNode(true),true);
$tmpDoc->appendChild($block);
$outer = $tmpDoc->saveHTML();
//this will remove the outer tags
return substr($outer,strpos($outer,'>')+1,-(strlen($node->nodeName)+4));
}
比我快几秒钟。我已经发布了SimpleXML方式,尽管这是相当琐碎的。