Need help parsing an XML file!!
Posted: Fri Aug 28, 2009 2:51 pm
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:
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:
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
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>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;
}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