$innerXML in PHP

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
rocco2004
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2008 3:55 pm

$innerXML in PHP

Post by rocco2004 »

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $innerXML in PHP

Post by Christopher »

These objects implement Iterator so use foreach(). See the examples in the manual:

http://us2.php.net/manual/en/function.s ... ildren.php
(#10850)
rocco2004
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2008 3:55 pm

Re: $innerXML in PHP

Post by rocco2004 »

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $innerXML in PHP

Post by Christopher »

See the examples in the link above.
(#10850)
rocco2004
Forum Newbie
Posts: 5
Joined: Sun Aug 17, 2008 3:55 pm

Re: $innerXML in PHP

Post by rocco2004 »

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.
Post Reply