Forum Moderators: coopster

Message Too Old, No Replies

HelpNeeded Grabbing an xml result and parse the results into an array

         

talos

12:46 am on Aug 17, 2004 (gmt 0)

10+ Year Member



I need help on grabbing an xml result and parse the results into an array.

the xml file located at another server,
htp://ww.domain.com:8000/file.xml :

<?xml version="1.0" standalone="yes"?>
<!DOCTYPE DRSWMASERVER (View Source for full doctype...)>
- <DRSWMASERVER>
<ALIAS>268757</ALIAS>
<CURRENTLISTENERS>0</CURRENTLISTENERS>
<PEAKLISTENERS>2</PEAKLISTENERS>
<MAXLISTENERS>20</MAXLISTENERS>
<BITRATE>0</BITRATE>
<HITS>0</HITS>
</DRSWMASERVER>

tried XPath.class.php but..^%^$%$@##@#@

i appreciate any help

Thanks in advance!

Warboss Alex

7:39 am on Aug 17, 2004 (gmt 0)

10+ Year Member



Try this class! I think it does what you want.

class XMLParser {

var $currentTag;
var $parsedData;

function XMLParser($data) {

$this->parsedData = array();

$this->currentTag = '';

$this->parseXML($data);

}

function startElement($parser, $name, $attrs){

$this->currentTag = $name;
}

function endElement($parser, $tag){

$this->currentTag = '';

}

function getInfo($parser, $data){

$this->parsedData[$this->currentTag] = $data;

}

function parseXML($data){

$xml_parser = xml_parser_create();

xml_set_object($xml_parser, &$this);

xml_set_element_handler($xml_parser, "startElement", "endElement");

xml_set_character_data_handler($xml_parser, "getInfo");

xml_parse($xml_parser, $data);

xml_parser_free($xml_parser);

}

function getParsedData() {

return $this->parsedData;

}
}

Call it like this:
$xml="<login><username>foo</username><password>bar</password></login>";

$parser =& new XMLParser($xml);

print_r($parser->getParsedData());

[edited by: Warboss_Alex at 7:48 am (utc) on Aug. 17, 2004]

Warboss Alex

7:40 am on Aug 17, 2004 (gmt 0)

10+ Year Member



Forgot to add. In your case, the $xml variable'd be your remote file. :)

talos

10:15 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



Warboss Alex could you be more helpfull cause i am not an expert. thanks.

Warboss Alex

6:15 am on Aug 18, 2004 (gmt 0)

10+ Year Member



Well, try the script I gave you! It takes an XML file and puts its elements into an array.. isn't that what you want?

Lpe04

6:48 am on Aug 18, 2004 (gmt 0)

10+ Year Member



Warboss, do you know how to get around a firewall to retrieve an XML file. Basically I think my host server has a firewall preventing me from accessing any outside XML files. I have heard something about fsocketopen or something like that. Thanks.

Warboss Alex

9:13 am on Aug 18, 2004 (gmt 0)

10+ Year Member



If there's a firewall, there's a good reason for it! Ask the server admin about it, not me.

Timotheos

3:35 pm on Aug 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I hope this isn't too much of a code dump but this is how I did something similar to parse a United States postal address from the USPS website. Maybe you could study it and just modify it for your purpose. Basically it opens a socket, I then use fputs to send a url with the address to check. What comes back in xml which is parsed by me is either the corrected address by USPS or gives an error. Good luck.

<?php
// use this to keep track of which tag the parser is currently processing
$currentTag = "";
// Array that will eventually hold the address
$usps_address = array();

function startElement($parser, $name, $attrs) {
global $currentTag;
$currentTag = $name;
}

function endElement($parser, $name) {
global $currentTag;
// clear current tag variable
$currentTag = "";
}

// process data between tags
function characterData($parser, $data) {

global $currentTag;
global $usps_address;

switch ($currentTag) {
case "Address1":
$usps_address['address1'] = $data;
break;
case "Address2":
$usps_address['address2'] = $data;
break;
case "City":
$usps_address['city'] = $data;
break;
case "State":
$usps_address['state'] = $data;
break;
case "Zip5":
$usps_address['zip5'] = $data;
break;
case "Zip4":
$usps_address['zip4'] = $data;
break;
case "Description":
$usps_address['error'] = $data;
break;
default:
break;
}
}

// Open a socket to the website
$fp = @fsockopen("production.shippingapis.com", 80, &$errno, &$errstr, 300);

if($fp) {
$address1 = rawurlencode($HTTP_POST_VARS["address2"]);
$address2 = rawurlencode($HTTP_POST_VARS["address1"]);
$city = rawurlencode($HTTP_POST_VARS["city"]);
$state = $HTTP_POST_VARS["state"];
$zip = rawurlencode($HTTP_POST_VARS["zip"]);

$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify&XML=
<AddressValidateRequest%20USERID=\"**********\"%20PASSWORD=\"**********\">
<Address%20ID=\"0\"><Address1>" . $address1 . "</Address1>
<Address2>" . $address2 . "</Address2><City>" . $city . "</City>
<State>" . $state . "</State><Zip5>" . $zip . "</Zip5><Zip4></Zip4></Address></AddressValidateRequest>";

fputs($fp, "GET " . $url . " HTTP/1.0\n\n");

while(!feof($fp)) {
$result = fgets($fp,500);
$body .= $result;
}

// cut out the header
$body = substr($body, strpos($body, "<?xml"));

// initialize parser
$xml_parser = xml_parser_create();

// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE);

// parse the xml
xml_parse($xml_parser, $body, feof($fp));

// clean up
xml_parser_free($xml_parser);

// Assign any errors
if(isset($usps_address["error"]))
if ($usps_address["error"] == "That State is not valid.")
$errors["state"] = "USPS error - State is not valid";
elseif ($usps_address["error"] == "That is not a valid city.")
$errors["city"] = "USPS error - City is not valid";
else
$errors["address1"] = "That address could not be found";

// Assign returned xml data to my formvars
elseif(isset($usps_address["address1"]) ¦¦ isset($usps_address["address2"])) {
foreach ($usps_address as $key => $value)
$usps_address[$key] = ucwords(strtolower($value));
$formvars["address1"] = $usps_address["address2"];
$formvars["address2"] = $usps_address["address1"];
$formvars["city"] = $usps_address["city"];
$formvars["state"] = strtoupper($usps_address["state"]);
$formvars["zip"] = $usps_address["zip5"] . "-" . $usps_address["zip4"];
}
}
?>