0

Even after going through too many posts, I couldn't find a way to parse this XML in PHP:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <SingleResponse xmlns="http://content.host.com">
         <ContentCafe DateTime="2013-02-18T04:31:16.1619171-05:00">
            <RequestItems UserID="abcdefg" Password="hijklmnop">
               <RequestItem>
                  <Key Type="ISBN" Original="9780002571838">9780002571838</Key>
                  <Content>ProductDetail</Content>
                  <ProductItems>
                     <ProductItem ID="8714731">
                        <ISBN>9780002571838</ISBN>
                        <Title>Memoirs</Title>
                        <Author>Rees-Mogg, William</Author>
                        <Source Code="BTB">B&amp;T Books</Source>
                        <Product Code="BOOK">Book</Product>
                        <Supplier Code="HARPE">HarperCollins</Supplier>
                     </ProductItem>
                  </ProductItems>
               </RequestItem>
            </RequestItems>
         </ContentCafe>
      </SingleResponse>
   </soap:Body>
</soap:Envelope>

I came until this point:

$xml = new SimpleXMLElement($xml);

but don't know how to proceed further reading the Title, Author and other information. Greatly appreciate any help that you could provide.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
Nirmal
  • 9,391
  • 11
  • 57
  • 81

2 Answers2

1

For your specific case, you can use SOAP Client getTypes () to fetch the struct and then design the code to handle the string returned from doRequest() method.

I guess, you should not be treating the response from a Webservice in terms of serialized XML data. Instead, use standard APIs to wrap it and just consider it as calling an API functional call. And process the return data rather than processing the entire HTTP packet data.

These resources might help . See this IBM's tutorial and also the PHP Soap Client Manual

Jay
  • 690
  • 1
  • 10
  • 27
1

It can be done like this:

Demo

$obj = new SimpleXMLElement($xml);

$item = $obj->children('http://schemas.xmlsoap.org/soap/envelope/')->children()->SingleResponse->ContentCafe->RequestItems->RequestItem->ProductItems->ProductItem;

echo $item->Title . "\n";
echo $item->Author . "\n";
echo $item->attributes()->ID;

Outputs

Memoirs
Rees-Mogg, William
8714731

If you have multiple <ProductItem> you will have to loop them instead of directly accessing them as I have done.

MrCode
  • 63,975
  • 10
  • 90
  • 112