PHP SimpleXML get innerXML
To the best of my knowledge, there is not built-in way to get that. I'd recommend trying SimpleDOM, which is a PHP class extending SimpleXMLElement that offers convenience methods for most of the common problems.
include 'SimpleDOM.php';
$qa = simpledom_load_string(
'<qa>
<question>Who are you?</question>
<answer>Who who, <strong>who who</strong>, <em>me</em></answer>
</qa>'
);
echo $qa->answer->innerXML();
Otherwise, I see two ways of doing that. The first would be to convert your SimpleXMLElement
to a DOMNode
then loop over its childNodes
to build the XML. The other would be to call asXML()
then use string functions to remove the root node. Attention though, asXML()
may sometimes return markup that is actually outside of the node it was called from, such as XML prolog or Processing Instructions.
How to get the innerText of an element with SimpleXml
In order for the traversal
echo $xml->note->body;
To work, your markup would need to be
<note>
<note>
<body>
…
To avoid such errors, it is good practise to name the variable you simplexml_load_file
into after the root element in the markup, e.g.
$note = simplexml_load_string($xml);
To get the "innerText" of a SimpleXmlElement you can do:
echo strip_tags($note->body->asXml());
the asXML()
method will give you the "outerXML" which you then remove with strip_tags
.
The alternative would be importing the node into DOM and then getting it's nodeValue
echo dom_import_simplexml($note->body)->nodeValue;
PHP retrieve xml node value with simplexml
As suggested by @Tomalak, this is a duplicate of Stackoverflow: PHP SimpleXML get innerXML
and you cannot do better than suggested with simplexml API.
function simplexml_innerXML($xml)
{
$content="";
foreach($node->children() as $child)
$content .= $child->asXml();
return $content;
}
In your code, replace $body_content = $el->asXml();
with $body_content = simplexml_innerXML($el);
However, you could also switch to another API that ofers distinction between innerXML
(what you are looking for) and outerXML
(what you get for now). Microsoft Dom libary offers this distinction but unfortunately PHP DOM doesn't.
I found that PHP XMLReader API
offers this distintion. See readInnerXML(). Though this API has quite a different approach to processing XML. Try it.
Finally, I would stress that XML is not meant to extract data as subtrees but rather as value. That's why you running into trouble finding the right API. It would be more 'standard' to store HTML subtree as a value (and escape all tags) rather than XML subtree. Also beware that some HTML synthax are not always XML compatible ( i.e. <br> vs ,<br/>
). Anyway in practice, you approach is definitely more convenient for editing the xml file.
PHP simplexml get image display
You should do something like this instead:
foreach ($xml->children() as $child)
{
echo '<img src="' . $child['path'] . '" alt="gallery image" />';
}
XML child nodes in xPath SimpleXMLElement
You cannot just use json_encode
on the xmlelement list that you get with your xpath. The xmlelement list is an array of xmlelements. And json_encode forces the xml elements to do a ->__toString()
, returning only texts directly inside the element. (And not of the child elements.)
So in your example the code will return a .
with some whitespaces, as your xpath returns an xmlelement list with just 1 element (the <p>
), and that one has only child elements and some text (whitespaces and a .
)
To get the inner xml of the elements, you should use ->asXML()
:
$xml = simplexml_load_string($xmlstring);
$elements = $xml->xpath('//Note[@id="f2"]/P');
foreach ($elements as $element) {
echo $element->asXML();
}
This prints:
<P><Pubref>[<Year>1965</Year>] <Series>VR</Series> <Pages>204</Pages></Pubref>.</P>
Related Topics
Executing Multiple SQL Queries in One Statement with PHP
Echo a Multidimensional Array in PHP
Move_Uploaded_File() Function Is Not Working
Php.Ini Is Nonexistent Loaded Configuration File (None)
Generating Random Numbers Without Repeats
How to Zip Folder Without Full Path
PHP Session Side-Effect Warning with Global Variables as a Source of Data
Simple HTML Dom: How to Remove Elements
How to Get Beanstalkd Queue to Work for PHP
Trying to Access Array Offset on Value of Type Bool in PHP 7.4
Run Shell Command and Send Output to File
Uploading in Codeigniter - the Filetype You Are Attempting to Upload Is Not Allowed
Php: Multiple SQL Queries in One MySQL_Query Statement
How to Get Output of Proc_Open()