using PHP to extract data from XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
x2i
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 4:01 pm

using PHP to extract data from XML

Post by x2i »

Hey I am trying to figure out how to extract individual data from an XML document. So far I have the following example which extracts information from XBOX LIVE and displays it as XML: http://www.x2i4eva.com/xbox/test2.php?gamertag=x2i4eva

I am using this script (the $_GET obtains the value inserted into the textbox on the example page i just gave):

Code: Select all

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag=".$_GET["tag"]);
 
$x = $xmlDoc->documentElement;
 
foreach ($x->childNodes AS $item) {
    print $item->nodeName . " = " . $item->nodeValue . "<br />";
}
?>
However that just displays the whole document as a rather unfriendly block of text, I would like to extract some of the information from that such as Gamertag and TileUrl.

Any help will be greatly appreciated.

Dan
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: using PHP to extract data from XML

Post by Ambush Commander »

Check out DOMElement->getElementsByTagName
x2i
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 4:01 pm

Re: using PHP to extract data from XML

Post by x2i »

Ambush Commander wrote:Check out DOMElement->getElementsByTagName
I dont think this would work, you see I am linking to an external XML document (that which I dont have access to).

As the results at http://www.x2i4eva.com/xbox/test2.php?gamertag=x2i4eva display as PHP and not as XML I dont think it would work using DOMElement.getElementsByTagName(name).

Unless theres a way to display the XML from this address: http://duncanmackenzie.net/services/Get ... ag=x2i4eva and then use DOMElement.getElementsByTagName(name) to get what I want?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: using PHP to extract data from XML

Post by Ambush Commander »

When you called DOMDocument->load() with the external URL, PHP went and fetched the data from the website and created an internal representation of it. You now have a local copy that you can manipulate.
x2i
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 4:01 pm

Re: using PHP to extract data from XML

Post by x2i »

okay, this isn't displaying anything right now. The reason being I think is that as you can see here: http://www.x2i4eva.com/xbox/test2.php?gamertag=x2i4eva, the XML document is parsed using the PHP load() function and is displayed as such:

#text =
Gamertag = x2i4eva
#text =
ProfileUrl = http://live.xbox.com/member/x2i4eva
etc...

This isnt XML anymore as you can see, therefore getting the elements by tag name doesn't seem to work as there aren't any tags present. However loading the XML document in this way seems to be the only way I can do it.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: using PHP to extract data from XML

Post by Ambush Commander »

Can we see your updated PHP code?
x2i
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 4:01 pm

Re: using PHP to extract data from XML

Post by x2i »

Ambush Commander wrote:Can we see your updated PHP code?
Okay so far I have the following code:

Code: Select all

 
<?php
$file  = file_get_contents("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag=x2i4eva");
print nl2br(htmlentities($file));
?>
 
That seems to print out the XML Document quite nicely. Now I Need to be able to choose say 'Gamertag' and it display 'x2i4eva' etx. I need to specify what I want to show if that makes sense. Preferably in PHP!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: using PHP to extract data from XML

Post by Ambush Commander »

Since you're not listening to what I'm telling you, use preg_match().

Code: Select all

 
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag=".$_GET["tag"]);
$tile_url = trim($xmlDoc->getElementsByTagName('TileUrl')->item(0)->nodeValue);
 
x2i
Forum Newbie
Posts: 5
Joined: Mon Jun 16, 2008 4:01 pm

Re: using PHP to extract data from XML

Post by x2i »

Ambush Commander wrote:Since you're not listening to what I'm telling you, use preg_match().

Code: Select all

 
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://duncanmackenzie.net/services/GetXboxInfo.aspx?GamerTag=".$_GET["tag"]);
$tile_url = trim($xmlDoc->getElementsByTagName('TileUrl')->item(0)->nodeValue);
 
Wow it actually worked and was so simple. I was listening and I did try what you told me but couldn't get it to work so tried other methods - perhaps I was on the wrong tracks. Anyway Thank you very much for this. I very much appreciate your help.

Dan
Post Reply