• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

how to use this xml data in php vars please

i have:

<?xml version="1.0" encoding="ISO-8859-1"?>
<items>
<item>
<zip>43040</zip>
<city>MARYSVILLE</city>
<state>OH</state>
</item>
</items>

id like to assign vars of

$Zip = "43040";
$City - "MARYSVILLE";
$State = "OH";

but grab the info form the xml (mind you this is a pulled xml from a url (it displays the above code when quarried)

Thank you in advance for any help and code you may provide
0
Johnny
Asked:
Johnny
  • 3
  • 2
1 Solution
 
hernst42Commented:
Use the simplexml extension for that like:

$xmlstr = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<items>
<item>
<zip>43040</zip>
<city>MARYSVILLE</city>
<state>OH</state>
</item>
</items>
XML;
$xml = new SimpleXMLElement($xmlstr);
$Zip = $xml->item[0]->zip;
$City = $xml->item[0]->city;
.....
0
 
JohnnyAuthor Commented:
not exactly what i was looking for.

im calling the info from another external page i need to know how to parse the info to vars.

ie

$url = "http://www.test.com/test/test_zip.php?Zip_Code=43040";
$text = file_get_contents($url);
echo $text;
echo '<BR>';
// if return looks like 'var,var,var' parse like below
$tokens = explode(',', $text);
$zip = $tokens[0];
$city = $tokens[1];
$state = $tokens[2];

echo 'Zip: '.$zip.'<BR>';
echo 'City: '.ucwords(strtolower($city)).'<BR>';
echo 'State: '.$state.'<BR>';

---------------------
thats how to do it if there in a comma delimited string for a return i need it to read an xml returned file like above and assign the vars.

*am i clear now or do you need more explanation*?? thank you for the reply tho
0
 
JohnnyAuthor Commented:
<?php

    // version:

$ZipCode='06776';
$url = "http://www.test.com/test/test_zip.php?Zip_Code='.$ZipCode;
$xmlstr = file_get_contents($url);
echo $xmlstr;
echo '<BR>';

$xml = new SimpleXMLElement($xmlstr);
$Zip = $xml->item[0]->zip;
$City = $xml->item[0]->city;

echo 'Zip: '.$zip.'<BR>';
echo 'City: '.ucwords(strtolower($city)).'<BR>';
echo 'State: '.$state.'<BR>';


?>

thats sorta what im looking for but it does not populate the vars there blank.
0
 
hernst42Commented:
> thats sorta what im looking for but it does not populate the vars there blank.
Yes because you misspelled he variables. It should be:
echo 'Zip: '.$Zip.'<BR>';
echo 'City: '.ucwords(strtolower($City)).'<BR>';

(notice the upper case letter at the beginning of the Variables names)
0
 
JohnnyAuthor Commented:
thank you very much. typos grrr
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now