简体   繁体   中英

How to extract node data from XML

I am trying to extract a node (called identifierValue) from this XML

Couple of things that I tried:

$NewXML = new SimpleXMLElement($response);
$ABN = $NewXML->xpath('//ABRPayloadSearchResults/response/searchResultsList/searchResultsRecord/ABN/identifierValue');

And

$ABN = $NewXML->ABRPayloadSearchResults[0]->response->searchResultsList->searchResultsRecord->ABN->identifierValue;

but not getting any results. I tried the same methods with a simple dummy XML and it works.

Any idea why this is failing for my XML?

I expect your problem is that your XPath query uses element names which are not in a namespace, whereas the elements in your XML are in a namespace.

If you associate a prefix (eg "abr") with the namespace URI used in the XML (ie http://abr.business.gov.au/ABRXMLSearchRPC/literalTypes ) using the function described here https://www.php.net/manual/en/simplexmlelement.registerxpathnamespace.php , then you should be able to query with an XPath expression that uses that prefix, eg

//abr:ABRPayloadSearchResults/abr:response/abr:searchResultsList/abr:searchResultsRecord/abr:ABN/abr:identifierValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM