We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,657 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Simple XML Parsing With PHP?

hi guys, how are you?

I hope someone can help me with this as its been bugging me all day, and I'm sure there is a very simple explanation.

Basically, what I have at the moment is this:-

$ip = ($_SERVER["REMOTE_ADDR"]);
$xml = file_get_contents("http://xxxxxxxxxxxxxx);

This returns me the following in XML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<statusCode>OK</statusCode>
	<statusMessage></statusMessage>
	<ipAddress>2.99.203.40</ipAddress>
	<countryCode>UK</countryCode>
	<countryName>UNITED KINGDOM</countryName>
</Response>

What I want to be able to do now, is store the countryCode in a variable called $country.

Surely it can't be so difficult??? I'm so stuck. I've tried this, and think i'm on the right track, but its not working.

$xmlparse = simplexml_load_file($xml);
$country=$xmlparse->countryCode[0];
// show country
echo $country;

Any help is much appreciated.

Regards
David

3
Contributors
4
Replies
2 Hours
Discussion Span
1 Year Ago
Last Updated
5
Views
sess4561
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

$country=$xmlparse->Response->countryCode;

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

$country=$xmlparse->Response->countryCode;

Thanks chris, i tried that, now getting an error:-

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0" encoding="UTF-8"?> <Response> <statusCode>OK</statusCode> <statusMessage></statusMessage> <ipAddress>2.99.203.40</ipAddress> <countryCode>UK</countryCode> <countryName>UNITED KINGDOM</countryName> </Response>" in /home/rankwar2/public_html/index.php on line 6

sess4561
Newbie Poster
2 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Are you sure the server is running PHP 5?

GliderPilot
Posting Whiz in Training
239 posts since Sep 2006
Reputation Points: 34
Solved Threads: 42
Skill Endorsements: 12

Hi,

You only use countryCode[0], when there are multiple countryCode .

for example,

<?xml version="1.0" encoding="UTF-8"?>
<Response>
<statusCode>OK</statusCode>
<statusMessage></statusMessage>
<ipAddress>2.99.203.40</ipAddress>
<countryCode>UK</countryCode>
<countryCode>UK1</countryCode>
<countryCode>UK2</countryCode>
<countryName>UNITED KINGDOM</countryName>
</Response>

If I want to parse the coutryCode for UK, my script should be something like this.Assuming that the above file is named xml.xml.


!ADDED LATER ON EDIT!

$xml = "xml.xml";
$xml = @simplexml_load_file($xml);

foreach($xml as $items){

$country = $items->countryCode[0];

echo $country;
}

If you need more example, please visit my previous posts in this forum... I got a lot about xml parsing.

veedeoo
Master Poster
735 posts since Oct 2011
Reputation Points: 298
Solved Threads: 129
Skill Endorsements: 13

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.2482 seconds using 2.69MB