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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.