2016-03-04 84 views
2

您好我想从这些肥皂消息中获取元素名称以确定要采取的操作但是我得到正文在这种情况下accountInformationRequest是我之后。我是在一个单一的URL获取多个请求因而需要获得元素name.These是我试图如何使用simplexml在soap消息中获取元素名称

$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> 

回答

0

你的XPath特别要求在命名空间http://schemas.xmlsoap.org/soap/envelope/名为Body元素,因此调用getName()上它找到的元素将始终返回名称“Body”。

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