View Full Version : Parsing XML
Pirate
04-10-2005, 11:53 AM
Ok, my project continues and I'm stuck on the part of converting an XML into usuable code. What I mean is the XML is converted into PHP or HTML that I can display as a search result.
This is what my result looks likeHTTP/1.1 200 OK
Connection: close
Date: Tue, 04 Oct 2005 00:40:56 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 1502
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><NADSearchResponse xmlns="http://202.125.163.83/FCSSearchWS/FCSSearch.asmx"><NADSearchResult><NAD><NADItem><PIN>55353697</PIN><HIN>10371513</HIN><GIVEN_NAME_1>BOB</GIVEN_NAME_1><GIVEN_NAME_2 /><GIVEN_NAME_3 /><SURNAME>SMITH</SURNAME><AD1>blah</AD1><FUN /><TN1>26</TN1><THN>FINCH</THN><THT>ST</THT><LOC>BINGARA</LOC><STT>NSW</STT><PCD>2404</PCD><BG1 /><PDN /><ALN /><SOURCE>PHONE</SOURCE><SOURCEDATE>27/10/1994 12:00:00 AM</SOURCEDATE></NADItem><NADItem><PIN>55578895</PIN><HIN>20966872</HIN><GIVEN_NAME_1>BOB</GIVEN_NAME_1><GIVEN_NAME_2 /><GIVEN_NAME_3 /><SURNAME>SMITH</SURNAME><AD1>blahDR</AD1><FUN /><TN1>8</TN1><THN>HARRY GRAHAM</THN><THT>DR</THT><LOC>MT KEMABLASJ</LOC><STT>NSW</STT><PCD>2000</PCD><BG1 /><PDN /><ALN /><SOURCE>PHONE</SOURCE><SOURCEDATE>1/03/2004 12:00:00 AM</SOURCEDATE></NADItem><NADItem><PIN>25166855</PIN><HIN>10865892</HIN><GIVEN_NAME_1>BOB</GIVEN_NAME_1><GIVEN_NAME_2 /><GIVEN_NAME_3 /><SURNAME>SMITH</SURNAME><AD1>BLAHAVE</AD1><FUN /><TN1>29</TN1><THN>THOMPSON</THN><THT>AVE</THT><LOC>ST JOHNS</LOC><STT>NSW</STT><PCD>2760</PCD><BG1 /><PDN /><ALN /><SOURCE>NAD</SOURCE><SOURCEDATE>1/07/2003 12:00:00 AM</SOURCEDATE></NADItem></NAD><Credit><Total>1040</Total></Credit></NADSearchResult></NADSearchResponse></soap:Body></soap:Envelope>I'm using nusoap, and I think it can be parsed back through it but I can't find any examples of this that I can learn from.
I'm now thinking that I need to get my script to find the values from the XML and turn them into variables in my php script from which I can generate the pages layout.
Does anyone know where I can find some examples of this or any parsing code for PHP?
Pirate
04-10-2005, 12:27 PM
aaah I think I figured it out.. the nusoap is bring an array back already, so I just need to encorperate that into it.
Directed
04-10-2005, 12:33 PM
PHeeewww, we can all rest easy now.
Spingo
04-10-2005, 12:50 PM
Alternatively, investigate XSLT ;)
Use that, and you can convert just about any XML data into nice HTML/CSS stuffs.
Colonel Kurtz
04-10-2005, 12:54 PM
It lives
Pirate
04-10-2005, 12:56 PM
Alternatively, investigate XSLT ;)
Use that, and you can convert just about any XML data into nice HTML/CSS stuffs.I tried that using some example files, just seeing if my server here would do it. But it wouldn't work. Bloody thing.
The array thats been spat out my nusoap is a bit weird, not sure how I pull the data out of it as it looks heaps different from the arrays I've been using.
dwarfthrower
04-10-2005, 12:58 PM
Or alternatley... if, like me, you strike something that doesn't return an array, but a plain lump of XML, You can parse it back into an array like so:
<?php
/*
* $Id: wsdlclient1.php,v 1.2 2004/03/15 23:06:17 snichol Exp $
*
* WSDL client sample.
*
* Service: WSDL
* Payload: document/literal
* Transport: http
* Authentication: none
*/
require_once('../lib/nusoap.php');
$proxyhost = isset($_POST['proxyhost']) ? $_POST['proxyhost'] : '';
$proxyport = isset($_POST['proxyport']) ? $_POST['proxyport'] : '';
$proxyusername = isset($_POST['proxyusername']) ? $_POST['proxyusername'] : '';
$proxypassword = isset($_POST['proxypassword']) ? $_POST['proxypassword'] : '';
$client = new soapclient('http://www.webservicex.com/globalweather.asmx?WSDL', true,
$proxyhost, $proxyport, $proxyusername, $proxypassword);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
// Doc/lit parameters get wrapped
$strCountry = 'Australia';
$param = array('CountryName' => $strCountry);
$result = $client->call('GetCitiesByCountry', array('parameters' => $param), '', '', false, true);
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Use the XML Parser to Map the XML back to useful PHP Variables
$strResultXML = $result['GetCitiesByCountryResult'];
$p = xml_parser_create();
xml_parse_into_struct($p, $strResultXML, $vals, $index);
xml_parser_free($p);
//Now we have an array of values we can step through
$Cities = array();
$CityCount = 0;
foreach ($vals as &$tag) {
if($tag['tag'] == 'CITY'){
$Cities[$CityCount] = $tag['value'];
$CityCount++;
}
}
// Display the result as a HTML form element
echo "<select name=cityselect>";
foreach ($Cities as &$city) {
echo "<option value=";
echo $city;
echo ">";
echo $city;
echo "</option>";
}
echo "</select>";
}
}
?>
Pirate
04-10-2005, 02:23 PM
Once again thanks man. Can I ask where you got this?
I need to read up how it passes the values from the parsed info to the HTML... basically this part. $Cities = array();
$CityCount = 0;
foreach ($vals as &$tag) {
if($tag['tag'] == 'CITY'){
$Cities[$CityCount] = $tag['value'];
$CityCount++;
}
}
dwarfthrower
04-10-2005, 03:04 PM
I got the info for xml_parse_into_struct from here: http://au.php.net/xml and here: http://php.planetmirror.com/manual/en/function.xml-parse-into-struct.php
The rest I just hacked out - fairly rudimentary array processing.
Basically xml_parse_into_struct takes some XML that looks like:
<NewDataSet>
<Table>
<Country>Australia</Country>
<City>Archerfield Aerodrome</City>
</Table>
<Table>
<Country>Australia</Country>
<City>Amberley Aerodrome</City>
</Table>
<Table>
<Country>Australia</Country>
<City>Alice Springs Aerodrome</City>
</Table>
<Table>
<Country>Australia</Country>
<City>Brisbane Airport M. O</City>
</Table>
<Table>
<Country>Australia</Country>
<City>Coolangatta Airport Aws</City>
</Table>
<Table>
<Country>Australia</Country>
<City>Cairns Airport</City>
</Table>
...
...
...
</NewDataSet>
and turns it back into an array that looks like:
Array
(
[0] => Array
(
[tag] => NEWDATASET
[type] => open
[level] => 1
[value] =>
)
[1] => Array
(
[tag] => TABLE
[type] => open
[level] => 2
[value] =>
)
[2] => Array
(
[tag] => COUNTRY
[type] => complete
[level] => 3
[value] => Australia
)
[3] => Array
(
[tag] => TABLE
[value] =>
[type] => cdata
[level] => 2
)
[4] => Array
(
[tag] => CITY
[type] => complete
[level] => 3
[value] => Archerfield Aerodrome
)
[5] => Array
(
[tag] => TABLE
[value] =>
[type] => cdata
[level] => 2
)
[6] => Array
(
[tag] => TABLE
[type] => close
[level] => 2
)
[7] => Array
(
[tag] => NEWDATASET
[value] =>
[type] => cdata
[level] => 1
)
[8] => Array
(
[tag] => TABLE
[type] => open
[level] => 2
[value] =>
)
[9] => Array
(
[tag] => COUNTRY
[type] => complete
[level] => 3
[value] => Australia
)
[10] => Array
(
[tag] => TABLE
[value] =>
[type] => cdata
[level] => 2
)
[11] => Array
(
[tag] => CITY
[type] => complete
[level] => 3
[value] => Amberley Aerodrome
)
[12] => Array
(
[tag] => TABLE
[value] =>
[type] => cdata
[level] => 2
)
...
...
...
}
So you can either loop through the array - or access the array members by their index keys to get the values. Then display those values mixed in with the HTML markup you wanted to use on your page.
dwarfthrower
04-10-2005, 03:08 PM
Once again thanks man. Can I ask where you got this?
I need to read up how it passes the values from the parsed info to the HTML... basically this part. $Cities = array();
$CityCount = 0;
foreach ($vals as &$tag) {
if($tag['tag'] == 'CITY'){
$Cities[$CityCount] = $tag['value'];
$CityCount++;
}
}
Makes more sense if you look at the structure of the array I posted above.
Basically, when the XML is parsed back into an array, each element of the array has various properties that you can access - such as the tag name, the value etc.
In this case I'm looping through each value in my parsed array - checking to see if the 'tag' atribute is "CITY" (because I want to make a list of cities) and if it is a match - putting the 'value' part of the array (ie the name of the city) into my new Cities array.
Then you can loop back through the cities array to print out HTML in any form you like - a table, a list of links etc.
Pirate
04-10-2005, 03:31 PM
Thanks man, so if I was to do the same method as you, I would use this code to generate my drop down list. // Use the XML Parser to Map the XML back to useful PHP Variables
$strResultXML = $result['NADSearchResponse'];
$p = xml_parser_create();
xml_parse_into_struct($p, $strResultXML, $vals, $index);
xml_parser_free($p);
//Now we have an array of values we can step through
$Cities = array();
$CityCount = 0;
foreach ($vals as &$tag) {
if($tag['SURNAME'] == 'SMITH'){
$Cities[$CityCount] = $tag['PCD'];
$CityCount++;
}
} from an parsed array such as this (
[NADSearchResult] => Array
(
[NAD] => Array
(
[NADItem] => Array
(
[0] => Array
(
[PIN] => 55353697
[HIN] => 10371513
[GIVEN_NAME_1] => BOB
[GIVEN_NAME_2] =>
[GIVEN_NAME_3] =>
[SURNAME] => SMITH
[AD1] => 28 BINCH ST
[FUN] =>
[TN1] =>
[THN] => BOB
[THT] => ST
[LOC] => WINFARS
[STT] => NSW
[PCD] => 2404
[BG1] =>
[PDN] =>
[ALN] =>
[SOURCE] => PHONE
[SOURCEDATE] => 27/10/1994 12:00:00 AM
)And this should give me a list of the post codes.
dwarfthrower
04-10-2005, 04:01 PM
Kinda... except you may need an extra couple of levels of iteration because your array is a few levels deeper than mine.
something like:
$pCodes = array();
$pCodeCount = 0;
foreach($vals['NADSearchResult']['NAD']['NADItem'] as &$NADitem){
$pCodes[$pCodeCount] = $NADitem['PCD'];
$pCodeCount++;
}
Would fit with the array structure you've got up there - but that's a lot different to what I get from parsing your XML from the first post. Is that the array you're getting back from xml_parse_into_struct or the one nusoap is returning natively?
if you want to send me the code -> dwarfthrower@zgeek.com
Pirate
04-10-2005, 04:51 PM
Thanks again dude, I've dropped you an email. Oh and I was using the one supplied by nusoap (as far as I know).
dwarfthrower
04-10-2005, 05:03 PM
Thanks again dude, I've dropped you an email. Oh and I was using the one supplied by nusoap (as far as I know).
Yeah it's cool... I had a tool around with your code and sent back some of the ways to step through your array.
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.