Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to use this xml data in php vars please

Posted on 2007-08-03
5
Medium Priority
?
166 Views
Last Modified: 2008-03-06
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
Comment
Question by:Johnny
  • 3
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 19629266
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
 

Author Comment

by:Johnny
ID: 19629319
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
 

Author Comment

by:Johnny
ID: 19629335
<?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
 
LVL 48

Expert Comment

by:hernst42
ID: 19630126
> 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
 

Author Comment

by:Johnny
ID: 19630545
thank you very much. typos grrr
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question