Forum Moderators: coopster
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!
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]
<?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"];
}
}
?>