Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 993
jantimon Avatar asked Nov 10 '10 14:11

jantimon


1 Answers

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

like image 97
Shikiryu Avatar answered Sep 17 '22 09:09

Shikiryu