简体   繁体   English

如何从 XML 中提取节点数据

[英]How to extract node data from XML

I am trying to extract a node (called identifierValue) from this XML我正在尝试从此XML中提取一个节点(称为 identifierValue)

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.我用一个简单的虚拟 XML 尝试了相同的方法,它可以工作。

Any idea why this is failing for my XML?知道为什么这对我的 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.我希望您的问题是您的 XPath 查询使用不在命名空间中的元素名称,而 XML的元素在命名空间中。

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如果您使用此处描述的函数https://www.php将前缀(例如“abr”)与 XML 中使用的名称空间 URI(即http://abr.business.gov.au/ABRXMLSearchRPC/literalTypes )相关联。 net/manual/en/simplexmlelement.registerxpathnamespace.php ,那么您应该能够使用使用该前缀的 XPath 表达式进行查询,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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