Jump to content

XML parsing


tobeyt23

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/292843-xml-parsing/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498287
Share on other sites

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).
Link to comment
https://forums.phpfreaks.com/topic/292843-xml-parsing/#findComment-1498311
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.