Jump to content

Parsing XML


jholder

Recommended Posts

Hi All,

I'm attempting to parse some XML and am having some trouble It seems that I'm grabbing the elements correctly, but they don't seem to be assigning themselves. When I try to echo the value, there's nothing.

 

It's probably some novice error (i'm new to PHP), so any help would be greatly appreciated!

$fp = fopen("MY URL HERE", "r");
if ($fp)
{
	echo "Made the check!\n";
	while (!feof($fp)) $gamerfeed .= fgets($fp);

	if ($gamerfeed != '')
	{
		$dom = new DomDocument();
		$dom->recover=true;
		@$dom->loadXML($gamerfeed);
		$RecentGames = $dom->getElementsByTagName('ArrayOfGame')->item(0);
		$HasEvents = false;


		// Loop through each Game within RecentGames
		foreach($RecentGames->getElementsByTagName('Game') as $GameNode)
		{
			$HasEvents = true;
			// Get the Game Attributes from within RecentGames
			$Name      = $GameNode->getElementsByTagName('Title')->textContent;
                                echo($Name);
  

It doesn't echo the name!

 

Here's the XML I'm grabbing:

<?xml version="1.0"?>
<ArrayOfGame>
  <Game>
    <Id>NPWR00449_00</Id>
    <IdGameEurope>75</IdGameEurope>
    <Title>Uncharted: Drakes Fortune</Title>
  </Game>
</ArrayOfGame>

 

I'm still learning parsing, so sorry if it's obvious :) thanks!

john

Link to comment
Share on other sites

Instead of creating a variable for your XML why not load it straight into the DomDocument?

$dom = new DomDocument('1.0', 'utf-8');
$dom->load('path/to/your/file.xml');
// Loop through each Game
foreach($dom->getElementsByTagName('Game') as $GameNode) {
$HasEvents = true;
// Get the Game Attributes from within RecentGames
$name = ($GameNode->getElementsByTagName('Title')->item(0)->nodeValue);
echo($name.'<br/>');
}

Link to comment
Share on other sites

SimpleXML > DOMDocument, IMO :D

 

<?php 

$xml_string = '<?xml version="1.0"?>
<ArrayOfGame>
  <Game>
    <Id>NPWR00449_00</Id>
    <IdGameEurope>75</IdGameEurope>
    <Title>Uncharted: Drakes Fortune</Title>
  </Game>
</ArrayOfGame>';

// To get a file use
// $xml = new SimpleXMLElement( 'http://domain.com/path/to/xml.xml', 0, TRUE );
$xml = new SimpleXMLElement( $xml_string );

foreach( $xml as $game ) {

foreach( $game as $key => $value )
	echo $key . ' - ' . $value . '<br>';
echo '<br>';

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.