The XML you have:
<order>
XXX
<orderValue>YYY</orderValue>
</order>
is technically speaking not compatible with Simplexml in PHP. Even though Clément Malet outlined that this tiny fragment did work in the isolated example he presents in his answer, you're referring to the more general problem here that Simplexml is incompatible for data-access for more differentiated (and possible) arrangement of document nodes.
Don't get me wrong, the XML is valid and Simplexml will load it (it won't destroy the document neither). But the interface of SimpleXMLElement can't address the mixed child-nodes you have here to the level of detail you likely need in your case:
- a text-node
"XXX"
- an element-node
<orderValue>
Simplexml simplifies the access to data in the XML document by technically allowing a parent element to only have one type of child-nodes. So only elements or text(s).
For everything more differentiated there is the sister-library named Dom which works interchangeably. And by extending SimpleXMLElement you can even create some extension to your existing code here without changing too much of it.
Let's add a new method to SimpleXMLElement that returns all child-nodes of any element you're interested in (text-nodes and element-nodes) and name that method childNodes:
/**
* Extend SimpleXMLElement to access child-nodes more differentiated.
*
* Demonstration to work together with DOM sister library
*/
class MyXml extends SimpleXMLElement
{
public function childNodes() {
$nodes = [];
foreach(dom_import_simplexml($this)->childNodes as $node) {
if ($node instanceof DOMText) {
$nodes[] = $node->nodeValue;
} elseif ($node instanceof DOMElement) {
$nodes[] = simplexml_import_dom($node);
} // ... other node-types ignored in this example
}
return $nodes;
}
}
Depending on how you create your SimpleXMLElement you can pass some parameter telling which SimpleXMLElement class to use. Here my example with a string:
$buffer = <<<XML
<doc>
<order>
XXX <!-- XXX is never reachable as-is with pure SimpleXML -->
<orderValue>YYY</orderValue>
ZZZ <!-- same for ZZZ -->
</order>
</doc>
XML;
$xml = simplexml_load_string($buffer, 'MyXml');
I create the SimpleXMLElement with the simplexml_load_string
function. It has a second parameter where I can pass the classname to be used. This allows you then to use the added method:
var_dump($xml->order->childNodes()[0]); // first child-node string(11) "
XXX "
As you can see, you perhaps can't do it with "pure" Simplexml but you can extend the interface and benefit from the much more detailed access the DOM sister-library offers.