Click to See Complete Forum and Search --> : XML Parsing Address Data Need Help


alexus
02-15-2008, 04:53 PM
Hey, I just got crazy trying to figure out how to parse the address data from USPS.com can some one help me out?

I have to XML possibilities:

Address Error

<?xml version="1.0" ?>
- <AddressValidateResponse>
- <Address ID="0">
- <Error>
<Number>-2147219040</Number>
<Source>SOLServerTest;SOLServerTest.CallAddressDll</Source>
<Description>This Information has not been included in this Test Server.</Description>
<HelpFile />
<HelpContext />
</Error>
</Address>
</AddressValidateResponse>



Address Is Correct

<?xml version="1.0" ?>
- <AddressValidateResponse>
- <Address ID="0">
<Address2>6406 IVY LN</Address2>
<City>GREENBELT</City>
<State>MD</State>
<Zip5>20770</Zip5>
<Zip4>1440</Zip4>
</Address>
</AddressValidateResponse>



What I need to extract is the error # and description (+ identify is the returned XML is errro file meaning it has ERROR key, otherwise if no ERROR key found just extract the address details.

I used to do RSS prsels but theu dont have tags like <address id="0"> (the once with spaces) so i can handle this onee :(

Please help me...

alexus
02-16-2008, 10:26 AM
Ok I was used Parser found here (http://www.criticaldevelopment.net/xml/doc.php) and it works almot OK. It parses the XML with identical structure but when it comes to my XML file it just doenst do anything. As a matter of fact it cant go over <Address ID="0"> or simple <Address>... I suspect its because Address is some sort of predefined variable or something? One way or another it just prevents script from working :(

NogDog
02-16-2008, 10:55 AM
If using PHP5, take a look at the SimpleXML (http://www.php.net/simplexml) functions.

Znupi
02-16-2008, 10:56 AM
1. You might want to look into SimpleXML (http://php.net/simple-xml) functions. It's really easy to do it with them.

$xml = new SimpleXMLElement($url, NULL, TRUE); //$url is the url to the xml file..
if ($xml->AddressValidateResponse->Address->Error) {
echo "Error #" . $xml->AddressValidateResponse->Address->Error->Number . ": " . $xml->AddressValidateResponse->Address->Error->Description;
}
else {
echo $xml->AddressValidateResponse->Address->Address2 . " " . ->AddressValidateResponse->Address->City . ", " . ->AddressValidateResponse->Address->State;
}

2. You can do it with patterns, but that's a bit more complicated. If you don't have SimpleXML available, tell us and maybe we'll come up with a pattern solution :)

alexus
02-16-2008, 11:01 AM
:o but my server uses PHP4, and SimpleXML was introduced only in PHP5 :( :(

NogDog
02-16-2008, 11:25 AM
It's time for your server to upgrade: PHP 4 End of Life Announcement (http://www.php.net/#2007-07-13-1) (from July, 2007).

alexus
02-16-2008, 12:04 PM
Greate, thanks for putting bullet into my head :(
I still have to read damn XML undr current server. After I'm done i might start looking for replacemnet server

NogDog
02-16-2008, 12:46 PM
For something like this, I might forgo the whole XML parsing thing and just grab the bits that you need via regular expression functions, e.g. (untested):

$regex = '#<Error>.*<Number>(.*)</Number>.*<Description>(.*)</Description>.*</Error>#s';
if(preg_match($regex, $xml, $matches))
{
// error record
$errorNbr = $matches[1];
$errorDesc = $matches[2];
// do whatever needs to be done with an error...
}
else
{
// do the same sort of thing to grab the address pieces
// might be easier to do each piece in a separate preg_match()
}