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
https://forums.phpfreaks.com/topic/248222-parsing-xml/
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
https://forums.phpfreaks.com/topic/248222-parsing-xml/#findComment-1274642
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
https://forums.phpfreaks.com/topic/248222-parsing-xml/#findComment-1274746
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.