Get ALL content from an XML Node as a string
I'm sorry if the title was confusing. I'm working on an XML parser that will act similar as HTML, but make it easier for non coders. The parser will read a XML node, and print out corresponding HTML code. The difficulty I came up to is I want to be able to access all 开发者_StackOverflowthe content inside a node as a string (including any child nodes). Right now I'm using PHP's Simple XML.
<parent>
some plane text. some Plane text. <child prop="test" /> more plane text
</parent>
some plane text. some Plane text. <child prop="test" /> more plane text
From here I will do sub string searches that will pull out the child node and handle it differently. In simple, I want to print out some text, then read the child node, and continue printing out text till a new child node is read.
Thanks for the help!!
$xml->parent[0]->asXML();
will output
<parent>
some plane text. some Plane text. <child prop="test" /> more plane text
</parent>
It may include the XML declaration depending on the element. You can then strip the opening and closing tags.
You could do that with this function:
function xml_contents($node)
{
$str = $node->asXML();
$i = strlen($node->getName()) + 2;
return substr(substr($str, $i), 0, -1 - $i);
}
精彩评论