a
b
both $foo->textContent
and $foo->nodeValue
return a
This might be helpful. Using what I found here and here
$txt = "";
foreach($foo->childNodes as $node) {
if ($node->nodeType == XML_TEXT_NODE) {
$txt .= $node->nodeValue;
}
}
Try this code
$doc = new DOMDocument();
$doc->loadXML('<root><foo>a<bar>b</bar></foo><foo>bar</foo></root>');
$foos = $doc->getElementsByTagName('foo');
foreach($foos as $v){
echo $v->firstChild->wholeText.'<br />';
}
The firstChild
property of DOMNode
returns a DOMText
object as there is a "text node" before <bar>
in first <foo>
Use firstChild
:
$foo->firstChild->textContent;