How to get data from the NewDataSet tag NewDataSet ? The children and registerxpathnamespace methods returned empty arrays.

  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <NewDataSet xmlns=""> <Table diffgr:id="Table1" msdata:rowOrder="0"> <CMan_Code>47156</CMan_Code> ............. 

  • This is what the solution looks like in my case: $manufacturesResponse = curl_exec($ch); $manufacturesXml = new SimpleXMLElement($manufacturesResponse); echo $manufacturesXml->Returned_DataSet->children('diffgr', true)->diffgram->children(null)->NewDataSet->asXML(); $manufacturesResponse = curl_exec($ch); $manufacturesXml = new SimpleXMLElement($manufacturesResponse); echo $manufacturesXml->Returned_DataSet->children('diffgr', true)->diffgram->children(null)->NewDataSet->asXML(); - kaminarifox

1 answer 1

You can use the children method (attention to the second argument, because of which you couldn’t manage it yourself) to go to the namespace, go to the desired element, and then return to the global space using the same children method, something like this :

 $xml = <<<XML <?xml version="1.0" encoding="UTF-8" ?> <root> <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"> <NewDataSet xmlns=""> <Table diffgr:id="Table1" msdata:rowOrder="0"> <CMan_Code>47156</CMan_Code> </Table> </NewDataSet> </diffgr:diffgram> </root> XML; var_dump(simplexml_load_string($xml) ->children('diffgr', true) ->diffgram ->children(null));