XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
rocco2004
Forum Newbie
Posts: 5 Joined: Sun Aug 17, 2008 3:55 pm
Post
by rocco2004 » 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:
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);
}
}
}
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:
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]);
}
$innerXML always returns false.
Many thanks
Rocco
rocco2004
Forum Newbie
Posts: 5 Joined: Sun Aug 17, 2008 3:55 pm
Post
by rocco2004 » Fri Aug 22, 2008 11:20 am
Hello,
I tried using the DOM but I can't find out how exactly it's parsing the xml. Here is my code:
Code: Select all
$doc = DOMDocument::loadXML('<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>
<li><a href="#">Item 2.2.2</a> </li>
</ul></li>
<li>Item 3 <ul>
<li><a href="#">Item 3.1</a> </li>
</ul></li>
<li><a href="#">Item 5</a> <ul>
<li><a href="#">Item 5.1</a> </li>
<li><a href="#">Item 5.2</a> </li>
</ul></li>
</ul>');
$innerXML2 = '';
foreach ($doc->childNodes as $node) {
$innerXML2 .= $this->InnerXML($doc,$node);
}
///////////////// and my function
function InnerXML($doc,$node) {
$innerXML = '';
foreach($node->childNodes as $child)
$innerXML .= $doc->saveXML($child);
return $innerXML;
}
It's looping only once, so the $innerXML will always return the entire content of the $doc. I want to return the childs like:
Code: Select all
1. <li><a href="#">Item 1</a> </li>
2. <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><li><a href="#">Item 2.2.2</a> </li></ul></li>
3. <li>Item 3 <ul> <li><a href="#">Item 3.1</a> </li> </ul></li>
4. <li><a href="#">Item 4</a> <ul> <li><a href="#">Item 4.1</a> </li> <li><a href="#">Item 4.2</a> </li></ul></li>
I don't mind using any xml object, just to return the list above.
Many Thanks!
Rocco
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Fri Aug 22, 2008 12:30 pm
See the examples in the link above.
(#10850)
rocco2004
Forum Newbie
Posts: 5 Joined: Sun Aug 17, 2008 3:55 pm
Post
by rocco2004 » Fri Aug 22, 2008 2:27 pm
I tried but it doesn't explain how to get the inner XML using SimpleXMLElement. DOM have ->saveXML but I didn't find anything similar in SimpleXMLElement?
Thanks.