如何使用simplexml在soap消息中获取元素名称

时间:2016-03-04 12:06:01

标签: php web-services soap laravel-5 simplexml

您好我试图从这些肥皂消息中获取元素名称,以便确定要采取的行动,但是我在这种情况下得到了BodyInformationRequestRequest就在我之后。我正在收到多个请求因此,在单个URL上需要获取元素名称。这是我试过的

 $xml=simplexml_load_string($content);
$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
    $xml->registerXPathNamespace('ns', 'http://www.yyy.com/mct/tx/2.0');
    $xml->registerXPathNamespace('ns1', 'http://www.yyy.com/mct/ty/2.0');
    $xml->registerXPathNamespace('ns2', 'http://www.yyy.com/mct/tx/2.1');
    $xml->registerXPathNamespace('ns3', 'http://www.yyy.com/mct/ty/2.1');

 $bodies = $xml->xpath('env:Body');
 foreach($bodies as $body){

    echo $body->getName();

        $reply = $body->children('ns', TRUE)->accountInformationRequest;
}




//Soap message
        <soapenv:Envelope 
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:ns="http://www.yyy.com/mct/tx/2.0" 
    xmlns:ns1="http://www.yyy.com/mct/ty/2.0"
    xmlns:ns2="http://www.yyy.com/mct/tx/2.1"
    xmlns:ns3="http://www.yyy.com/mct/ty/2.1">
       <soapenv:Header/>
       <soapenv:Body>
          <ns:accountInformationRequest>
             <ns:security>
                <ns1:login>sam</ns1:login>
                <ns1:password>lin</ns1:password>
             </ns:security>
             <ns:hsTransactionId>001</ns:hsTransactionId>
             <ns:destinationUri>003</ns:destinationUri>
             <!--Optional:-->
             <ns:routingTag>B2B</ns:routingTag>
             <!--Optional:-->
             <ns2:vendorSpecificFields>
                <!--Zero or more repetitions:-->
                <ns3:vsf>
                   <ns3:vendorId>10</ns3:vendorId>
                   <ns3:fieldId>22</ns3:fieldId>
                   <ns3:value>2</ns3:value>
                </ns3:vsf>
             </ns2:vendorSpecificFields>
          </ns:accountInformationRequest>
       </soapenv:Body>
    </soapenv:Envelope>

1 个答案:

答案 0 :(得分:0)

您的XPath专门请求名称空间Body中名称为http://schemas.xmlsoap.org/soap/envelope/的元素,因此在找到的元素上调用getName()将始终返回名称“Body”。

您想要找到的是身体标签的孩子的名称;您可以将其构建到XPath中,例如->xpath('env:Body/*'),或者您可以遍历$body的孩子,例如foreach ( $body->children('http://www.yyy.com/mct/tx/2.0') as $child ) { echo $child->getName(); }