Need help parsing an XML file!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dsainteclaire
Forum Newbie
Posts: 5
Joined: Fri Aug 28, 2009 2:38 pm

Need help parsing an XML file!!

Post by dsainteclaire »

Hello,

I am really new to PHP. Normally I have done most of my server side development using Rails, but for my current project my employer asked me to test drive PHP to see if it's something we could use a little more often.

My company exposes a SOAP webservice, but does not publish a WSDL. For testing purposes I have a string containing all the XML needed to post the SOAP message and I've been using streams to post the message to the service. That's all sort of irrelevant though. I've gotten to where I can post the message, and I get the response back and can see it when I monitor web traffic so I know that the message arrives and I get a proper response. The issue I seem to be having is with the XML namespaces. Rails doesn't care about namespaces and the XPath method will let you pull any element you from from a document. However, it seems like PHP is doing a much more strict job of enforcing namespaces. Can someone help me figure out how to pull the XML out from the response. Here is what the response envelope looks like:

Code: Select all

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:prod="http://webservice.examplecompany.com/Productpriceservice">
<soap:Body>
<prod:productPriceResponse>
<prod:response>
<prod:productPrice>
<prod:sku>PROD1</prod:sku>
<prod:price>$349.99</prod:price>
</prod:productPrice>
</prod:response>
</prod:productPriceResponse>
</soap:Body>
</soap:Envelope>
Like I said, I can see this envelope coming back and if I just echo the response object I get the values of SKU and price without all the XML. However, I need to actually access these elements, otherwise I wouldn't be calling the service right?
This is what I tried:

Code: Select all

    $response = stream_get_contents($fp);
    
    if ($response === false){
        echo "Got nothing back";
    }
    else {
        echo "got something back";
        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0 );
        xml_parse_into_struct($parser, $response, $vals, $index);
        xml_parser_free($parser);
        echo "Values in XML response\n";
        print_r($vals);
        $xml = new SimpleXMLElement($response);
        //$xml = simplexml_load_string($response);
        $xml->registerXPathNamespace("prod","http://webservice.examplecompany.com/Productpriceservice");
        $price = $xml -> xpath("prod:productPrice/prod:price");
        echo $price;
    }
The first echo in the if block spits out the whole XML structure so I know that I'm getting back what I want, but when I try to use the xpath method to pull out individual elements it looks like I get nothing back.

Can someone help me figure out what I'm doing wrong? I'm pretty sure it's namespace related, but I don't really know how to resolve it, and don't really have a week to spend on it.

Thanks!
David
joeynovak
Forum Commoner
Posts: 26
Joined: Sun May 10, 2009 11:09 am

Re: Need help parsing an XML file!!

Post by joeynovak »

No fun... I usualy use the Expat parser... See: http://www.w3schools.com/PHP/php_xml_parser_expat.asp

I KNOW it's not the best, but it's simple (if your xml structure is pretty flat, which it looks like yours is).

Joey
dsainteclaire
Forum Newbie
Posts: 5
Joined: Fri Aug 28, 2009 2:38 pm

Re: Need help parsing an XML file!!

Post by dsainteclaire »

Ok, I think that your advice will really help me out, but it brings up a question that I haven't been able to find a very clear answer to. So the parser reads in the XML file and then calls the start function for each tag that it comes across. Then in sequence calls the data handler function and the stop function, right? My question is, can I skip the element handler function and just set a data handler function? Basically, I will be calling this service and the tags will always be in the same sequence, so I just need access to the data portion of the XML elements, so likely my element handler would be empty. Unless I'm totally missing something?!?

Thanks again for all your help!
Post Reply