tobeyt23 Posted December 2, 2014 Share Posted December 2, 2014 I can't seem to get this to parse into an array: <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:getReportTestResponse xmlns:ns="http://webservice.avm.pvads.com"> <ns:return xmlns:ax21="http://webservice.avm.pvads.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:WSReportBean"> <ax21:confidence /> <ax21:dateStamp>12/07/2007</ax21:dateStamp> <ax21:html>adsasdasdsadasd</ax21:html> <ax21:pdsReference /> <ax21:product>AVi (Exterior AND Interior Inspection) - 48hr RUSH</ax21:product> <ax21:sourceData /> <ax21:successful>true</ax21:successful> <ax21:value>168000.0</ax21:value> </ns:return> </ns:getReportTestResponse> </soapenv:Body> </soapenv:Envelope Tried simplexml_load_string and comes back with nothing. Any help would be great thanks. Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/ Share on other sites More sharing options...
hansford Posted December 2, 2014 Share Posted December 2, 2014 I don't know whether you just pasted without it or it was never there. But you are missing a closing tag. </soapenv:Envelope Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498281 Share on other sites More sharing options...
tobeyt23 Posted December 2, 2014 Author Share Posted December 2, 2014 Missed it when I copied and pasted. Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498282 Share on other sites More sharing options...
tobeyt23 Posted December 2, 2014 Author Share Posted December 2, 2014 This seems to be working: $xml = preg_replace("/(<\/?)(\w+)[^>]*>)/", "$1$2$3", $_xmlString); $xmlstring = simplexml_load_string($xml); $json = json_encode($xmlstring); $responseArray = json_decode($json,true); Does anyone have something else i should be doing? Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498287 Share on other sites More sharing options...
boompa Posted December 2, 2014 Share Posted December 2, 2014 Does anyone have something else i should be doing? Using the PHP SoapClient instead of doing XML parsing? Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498289 Share on other sites More sharing options...
tobeyt23 Posted December 2, 2014 Author Share Posted December 2, 2014 I tried doing that and it will not work. Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498291 Share on other sites More sharing options...
salathe Posted December 2, 2014 Share Posted December 2, 2014 Working with namespaced elements is annoying, with SimpleXML. You can recognise namespaces by xmlns:...="..." attributes, and <foo:name tags. A better option would be to use the DOM family of classes and functions. Here's a basic example that grabs the <ax21:...> elements from your XML and throws them into an array. <?php $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <ns:getReportTestResponse xmlns:ns="http://webservice.avm.pvads.com"> <ns:return xmlns:ax21="http://webservice.avm.pvads.com/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:WSReportBean"> <ax21:confidence /> <ax21:dateStamp>12/07/2007</ax21:dateStamp> <ax21:html>adsasdasdsadasd</ax21:html> <ax21:pdsReference /> <ax21:product>AVi (Exterior AND Interior Inspection) - 48hr RUSH</ax21:product> <ax21:sourceData /> <ax21:successful>true</ax21:successful> <ax21:value>168000.0</ax21:value> </ns:return> </ns:getReportTestResponse> </soapenv:Body> </soapenv:Envelope> XML; $document = new DOMDocument; $document->loadXML($xml); $ax21_elements = $document->getElementsByTagNameNS("http://webservice.avm.pvads.com/xsd", "*"); $array = array(); foreach ($ax21_elements as $element) { $array[$element->localName] = trim($element->textContent); } var_dump($array); That will output an array like: array( { ["confidence"]=> string(0) "" ["dateStamp"]=> string(10) "12/07/2007" ["html"]=> string(15) "adsasdasdsadasd" ["pdsReference"]=> string(0) "" ["product"]=> string(50) "AVi (Exterior AND Interior Inspection) - 48hr RUSH" ["sourceData"]=> string(0) "" ["successful"]=> string(4) "true" ["value"]=> string( "168000.0" } That said, there's usually no real need to "convert" XML into an array at all. Instead, work with the DOM objects (here $document, $ax21_elements, and $element are DOM objects). Quote Link to comment https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.