$innerXML in PHP
Posted: Tue Aug 19, 2008 7:04 pm
Hello,
I have to displays all the child nodes of the root element. I need to convert the following code from C++ to PHP:
The idea is to get the inner XML code from the first level child nodes. I tried to create the following code in PHP but it doesn't seem to be working properly:
$innerXML always returns false.
Many thanks
Rocco
I have to displays all the child nodes of the root element. I need to convert the following code from C++ to PHP:
Code: Select all
XmlDocument^ doc = gcnew XmlDocument;
doc->LoadXml("doc.xml");
XmlNode^ root = doc->FirstChild;
//Display the inner content of the child nodes.
if ( root->HasChildNodes ) {
for ( int i = 0; i < root->ChildNodes->Count; i++ ) {
Console::WriteLine( root->ChildNodes[ i ]->InnerXML);
}
}
}Code: Select all
$xmlDoc = new SimpleXMLElement('<ul> <li><a href="#">Item 1</a> </li> <li>Item 2<ul> <li>Item 2.1 <ul> <li><a href="#">Item 2.1.1</a> </li> <li><a href="#">Item 2.1.2</a> </li> </ul></li> <li><a href="#">Item 2.2</a> </li> <li><a href="#">Item 2.2.1</a> </li> </ul></li> <li>Item 3 <ul> <li><a href="#">Item 3.1</a> </li> <li><a href="#">Item 3.1</a> </li> </ul></li> </ul>');
for ($i = 0; $i < count($xmlDoc->children()); $i++){
$innerXML = $xmlDoc->saveXML($xmlDoc[$i]);
}Many thanks
Rocco