0

<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;               
}
4

2 に答える 2

1

XPath で検索できます。

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

$errors = $xml->xpath("//err:Errors");
echo $errors[0]->saveXML(); // prints the tree
于 2013-03-28T14:50:35.517 に答える
0

XML の複数のステップを一度にジャンプしようとしています。->演算子とメソッドは、任意の子孫ではなく、直接の children->children()を返すだけです。

他の回答で述べたように、XPath を使用して下にジャンプできますが、トラバースする場合は、渡す各ノードの名前空間に注意を払い、->children()変更されたときに再度呼び出す必要があります。

以下のコードは、それを段階的に分解します(ライブデモ):

$sx = simplexml_load_string($xml);
// These are all in the "soapenv" namespace
$soap_fault = $sx->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault;
// These are in the (undeclared) default namespace (a mistake in the XML being sent)
echo (string)$soap_fault->children(null)->faultstring, '<br />';
$fault_detail = $soap_fault->children(null)->detail;
// These are in the "err" namespace
$inner_fault_code = (string)$fault_detail->children('http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1')->Errors->ErrorDetail->PrimaryErrorCode->Code;               
echo $inner_fault_code, '<br />';

もちろん、その一部またはすべてを一度に実行できます。

$inner_fault_code = (string)
    $sx
    ->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->Fault
    ->children(null)->detail
    ->children('http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1')->Errors->ErrorDetail->PrimaryErrorCode->Code;               
echo $inner_fault_code, '<br />';
于 2013-03-28T18:36:41.463 に答える