jholder Posted October 1, 2011 Share Posted October 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/248222-parsing-xml/ Share on other sites More sharing options...
Buddski Posted October 1, 2011 Share Posted October 1, 2011 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/>'); } Quote Link to comment https://forums.phpfreaks.com/topic/248222-parsing-xml/#findComment-1274642 Share on other sites More sharing options...
xyph Posted October 1, 2011 Share Posted October 1, 2011 SimpleXML > DOMDocument, IMO <?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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248222-parsing-xml/#findComment-1274746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.