jantimon
jantimon

Reputation: 38142

Getting the XML content of a SimpleXMLElement

Is there a way to extract the "innerHTML" of a SimpleXMLElement?

For example how would I get '<div><h1>Hello World</h1></div>' out of the following XML Code using SimpleXMLElement:

<xml>
  <body1>
    <div><h1>Hello World</h1></div>
  </body1>
  <body2>
    <div><h2>Goodby Moon</h2></div>
  </body2>
</xml>

I read the documentation and all I came up with was $xml->body1->asXML();. It is almost the right solution but it also adds the root tag:

 <body1>
   <div><h1>Hello World</h1></div>
 </body1>

Is it possible to skip the surrounding body1 tags?


Edit:

The xml might also look like this:

<xml>
  <body1>
    <div><h1>Hello World</h1></div>
    <div><h1>Hello Moon</h1></div>
    Some Text
  </body1>
  <body2>
    <div><h2>Goodby Moon</h2></div>
  </body2>
</xml>

As in: http://codepad.org/8ZuRYZOW

Upvotes: 7

Views: 4140

Answers (2)

Shikiryu
Shikiryu

Reputation: 10219

If you're sure it's a div you're looking for :

$xml->body1[0]->div->asXML();

else

$xml->body1[0]->children()->asXML();

http://codepad.org/QUytim28 else http://codepad.org/hDt28BL6

Upvotes: 6

cwallenpoole
cwallenpoole

Reputation: 81988

You probably want the children method or you want to use $xml->body1[ 0 ]->asXML(); (array access)

Upvotes: 0

Related Questions