2

Below is the XML that I am trying to convert using simplexml_load_string().

$THE_XML = '<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
  <ns:getMarketsByNpaNxxResponse xmlns:ns="http://ws.inventory.ccds.com" xmlns:ax251="http://exceptions.commons.ccds.com/xsd" xmlns:ax250="http://exceptions.ccds.com/xsd" xmlns:ax260="http://availability.item.productsservices.ccds.com/xsd" xmlns:ax259="http://detail.item.productsservices.ccds.com/xsd" xmlns:ax264="http://item.productsservices.ccds.com/xsd" xmlns:ax257="http://ws.inventory.ccds.com/xsd" xmlns:ax255="http://security.ccds.com/xsd"><ns:return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax257:Market">
    <ax257:marketId>30</ax257:marketId>
    <ax257:marketName>AZ-FLAGSTAFF</ax257:marketName> 
    <ax257:npaNxxs>928863</ax257:npaNxxs>
    </ns:return>
  </ns:getMarketsByNpaNxxResponse>
</soapenv:Body>
</soapenv:Envelope>';


$RESULT = simplexml_load_string($THE_XML);

var_dump($RESULT);

Returns:

object(SimpleXMLElement)#3 (0) {
}

What am I doing wrong?

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
MrZ1818
  • 21
  • 2
  • That's not an empty object - `SimpleXMLElement` has its own implementation of `var_dump()` that doesn't usually show recursive nodes. It would be `(bool)false` if it failed to load. – Michael Berkowski Jan 20 '14 at 18:54
  • Do you know how to retrieve the namespaced child nodes? – Michael Berkowski Jan 20 '14 at 18:55
  • Can you show an example of how you would show the results of the returned XML object? – MrZ1818 Jan 20 '14 at 19:02
  • 1
    If you understand XML namespaces and how SimpleXML handles them, these should set you on your way. Some examples: http://stackoverflow.com/questions/6027398/php-simplexml-namespace-problem, http://stackoverflow.com/questions/6843025/simplexml-and-namespaces Important for you is to `$RESULT->children('soapenv', true)->Body->children('ns', true)->getMarketsByNpaNxxResponse->children('ax257', true)->marketName` – Michael Berkowski Jan 20 '14 at 19:32
  • Thanks that example really helped - but this is so coarse to parse - it seems overly difficult :/ – MrZ1818 Jan 20 '14 at 19:42
  • That's one of the big reasons JSON is winning out over XML these days. JSON REST services are so much more easily implemented on both client & server than SOAP/XML. – Michael Berkowski Jan 20 '14 at 19:50
  • @MichaelBerkowski Holy War time! ;) It's perfectly possible to write succinct XML that's not as pointlessly bloated as SOAP (and to use it with REST too!) It's also perfectly possible to write hideously confusing JSON, and I've seen people doing just that. Just sayin'... – IMSoP Jan 21 '14 at 23:32

0 Answers0