gpt4 book ai didi

php - SimpleXML 访问元素 - PHP

转载 作者:行者123 更新时间:2023-12-04 17:04:30 27 4
gpt4 key购买 nike

我试图进入 <err:Errors>位于下面的 SOAP 中。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<soapenv:Fault>
<faultcode>Client</faultcode>
<faultstring>An exception has been raised as a result of client data.</faultstring>
<detail>
<err:Errors xmlns:err="http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1">
<err:ErrorDetail>
<err:Severity>Hard</err:Severity>
<err:PrimaryErrorCode>
<err:Code>120802</err:Code>
<err:Description>Address Validation Error on ShipTo address</err:Description>
</err:PrimaryErrorCode>
</err:ErrorDetail>
</err:Errors>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>

这是我尝试这样做的方式,但 $fault_errors->Errors 没有任何内容。
$nameSpaces = $xml->getNamespaces(true);
$soap = $xml->children($nameSpaces['soapenv']);
$fault_errors = $soap->Body->children($nameSpaces['err']);

if (isset($fault_errors->Errors)) {
$faultCode = (string) $fault_errors->ErrorDetail->PrimaryErrorCode->Code;
}

最佳答案

您可以使用 XPath 进行搜索:

$ns = $xml->getNamespaces(true);
$xml->registerXPathNamespace('err', $ns['err']);

$errors = $xml->xpath("//err:Errors");
echo $errors[0]->saveXML(); // prints the tree

关于php - SimpleXML 访问元素 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684572/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com