Get the values of the child nodes with PHP DOM
dom, php, xml
Solution
Try
$document_xml = new DOMDocument();
$document_xml->loadXML($xml);
$elements = $document_xml->getElementsByTagName('LISTING');
foreach ($elements as $node) {
$idtest = $node->getElementsByTagName('DATEMAJ');
$idElem = $node->getElementsByTagName('ID');
$idList[] = $idElem->item(0)->nodeValue;
}
See demo here
Problem
Here is an example XML I use: ``` <LISTING diffgr:id="LISTING1" msdata:rowOrder="0" diffgr:hasChanges="inserted"> <ID>ACCAMAQU0470001P</ID> <DATECREA>2013-01-28T09:45:21+01:00</DATECREA> <DATEMAJ>2014-01-09T17:41:25+01:00</DATEMAJ> ... </LISTING> ... ``` In the PHP code, here I am: ``` $document_xml = new DOMDocument(); $document_xml->loadXML($retour['any']); $elements = $document_xml->getElementsByTagName('LISTING'); while ($elements->item($i)) { $element = $elements->item($i); // On obtient le nœud $list = $element->childNodes; // On récupère les nœuds avec childNodes $idtest = $element->getElementsByTagName('DATEMAJ'); $idElem = $element->getElementsByTagName('ID'); foreach($idElem as $idSirtaq){ $idList[] = $idSirtaq->firstChild->nodeValue; } foreach ($idtest as $test) { //HERE ... } ... } ``` I would to get the value of nodes "ID" and "DATEMAJ". I know to get the "DATEMAJ" value, with `$test->firstChild->nodeValue`, but not how to retrieve the value of the node "ID".