Jump to content

[SOLVED] How to write the inner HTML of an XML element


nothing500

Recommended Posts

This might be possible with "DOMDocument::saveHTML()", but I'd like to get by with "SimpleXML" if possible. I can output something almost as I want it with "asXML()", but that leaves me with those darn opening and closing tags from the XML element, when I just want the stuff inside the element. I tried looping through the child nodes with "children()".

 

$xml = '<tag>This is <em>some of</em> my content.</tag>';
$content = new SimpleXMLElement($xml);
echo $content->asXML();
// ACTUAL OUTPUT: <tag>This is <em>some of</em> my content.</tag>
// DESIRED OUTPUT: This is <em>some of</em> my content.

 

This seems like it should be easy. Anyone know?

Hallelueigah! .. that's a hard word to spell  >:(

 

XMLReader::innerXML() is the answer!

If you're having difficulty reading XML documents, have a look at XMLReader, PHP's recent pull-based XML parser for a very THOROUGH way of traversing your XML data. You'll need the "libxml" extension active, which is active on PHP >= 5.1 by default.

 

Works like this:

$reader = new XMLReader();
$reader->XML($some_xml_element);
$reader->next();
echo $reader->readInnerXML();

Thanks, me  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.