Jimmy M
Jimmy M

Reputation: 1627

Parsing data from echoed XML Response in PHP

I am working on a web-service integration project. Now, pass the request using cURL method and getting an XML response. Instead of saving the XML response file, i echoed the XML response in my page(thought it will save server response time). Now i am getting XML response in my page echoed like this,

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" >
<SOAP-ENV:Body>
   <CreateShipmentReply xmlns="http://www.parcelforce.net/ws/ship/v5">
     <CompletedShipmentInfo>    
         <Status>ALLOCATED</Status>
      <CompletedShipments>
        <CompletedShipment>
            <ShipmentNumber>MK0030912</ShipmentNumber>
        </CompletedShipment>
      </CompletedShipments>
      <RequestedShipment>
        <DepartmentId>1</DepartmentId>
        <ShipmentType>DELIVERY</ShipmentType>
        <ContractNumber>P664383</ContractNumber>
        <ServiceCode>SUP</ServiceCode>
        <ShippingDate>2012-05-25</ShippingDate>
        <RecipientContact>
           <BusinessName>BUSINESS NAME</BusinessName>

In order to save the data in XML into my database/table i need to parse it from the nodes. I am having the code for parsing the XML from a saved file, but couldn't find a solution for the same in echoed XML response. How can i read the data from an echoed XML response.

Need Help. Thanks in advance.

Upvotes: 1

Views: 2588

Answers (2)

s.webbandit
s.webbandit

Reputation: 17000

To work with XML use DOM PHP class. It's pretty easy to understand and very powerfull.

Just load your response XML by:

$dom = new DOMDocument;

$dom->loadXML($responsestring);

Then you can do whatever you want manipulating DOM though DOMDocument methods.

Upvotes: 1

Kasia Gogolek
Kasia Gogolek

Reputation: 3414

You might want to read up on SimpleXML which let's you convert xml documents into objects, making them easier to read and manipulate. i.e

$xml_object = new SimpleXMLElement($xmlstr);

Upvotes: 0

Related Questions