This question already has answers here:
Answer
Solution:
You need to fetch the parent nodes (texto
), iterate and save each child node as XML:
$documento = new SimpleXMLElement(getXMLstring());
foreach ($documento->xpath('//texto') as $texto) {
$result = '';
foreach ($texto->children() as $content) {
$result .= $content->asXML();
}
var_dump($result);
}
Output:
string(59) "<dl>
<dt>1. Poder adjudicador: </dt>
<dd>
[...]
</dd>
</dl>"
SimpleXML is an abstraction focused on element nodes. It has limits. If the texto
element can have non-element child nodes they will not be included. In this case you need to use DOM.
$document = new DOMDocument();
$document->loadXML(getXMLString());
$xpath = new DOMXpath($document);
foreach ($xpath->evaluate('//texto') as $texto) {
$result = '';
foreach ($texto->childNodes as $content) {
$result .= $document->saveXML($content);
}
var_dump($result);
}
Additionally DOMXpath::evaluate()
supports full Xpath 1.0, including expressions that return scalar values.
Source