Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get all the elements and values as array from the below soap response. But I am only getting elements but no values.

I have used this parser to get the elements and values. Thanks in advance.

Here is the code I am using,

  $response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <toy:SearchReq xmlns:toy="www.something.com/toy" xmlns:gen="www.something.com/gen">
         <gen: manufacturedIn="SomePlace"/>
         <toy:SearchToy>
            <toy:SearchType>
               <gen:Shop Code="WALMART"/>
            </toy:SearchType>    
         </toy:SearchToy>
         <toy:SearchToy>
            <toy:SearchType>
               <gen:Shop Code="AMAZON"/>
            </toy:SearchType>    
         </toy:SearchToy>
      </toy:SearchReq>
   </soapenv:Body>
</soapenv:Envelope>';

$doc = simplexml_load_string($response);

$path = array($doc
    ->children('http://schemas.xmlsoap.org/soap/envelope/')
    ->Body
    ->children('www.something.com/toy'));


foreach($path as $elem){
    print_r($elem);
    $elemChild = $elem->children('www.something.com/gen');
    var_dump($elemChild);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
541 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can read SOAP messages by registering the namespace - you only need the namespace bit if you care about the SOAP parts of the message.

$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//toy') as $toy)
{
    print_r($toy);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...