Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Displaying attribute node. *Newbie Alert*

12:19 am on Aug 11, 2010 (gmt 0)

New User

5+ Year Member

joined:Aug 11, 2010
votes: 0

Hi there

I've only just started dabbling with XML - so far I've managed to display an XML file's data in reverse order with some zebra striping.

I'm stuck on trying to display an attribute node as follows - I need help to find the gameID! I know my code is wrong here - any help appreciated!



$gameIDs = $game->getAttributeNode("game_id");
$gameID = $gameIDs->item(0)->nodeValue;


<game game_id="game456">

$z = 0;
$doc = new DOMDocument();
$doc->load( 'xmlfile.xml' );

$games = $doc->getElementsByTagName("game");
$num = $games->length;

for ($i = $num-1; $i >= 0; $i--){

$game = $games->item($i);

$names = $game->getElementsByTagName("name");
$name = $names->item(0)->nodeValue;

$gameIDs = $game->getAttributeNode("game_id");
$gameID = $gameIDs->item(0)->nodeValue;

$times = $game->getElementsByTagName("start_time");
$time = $times->item(0)->nodeValue;

$atomFormattedDate = $time;
$phpTimestamp = strtotime(substr($atomFormattedDate, 0, 10).' '.substr($atomFormattedDate, 11, 8));

?><tr class="<?=($z++%2==1) ? 'odd' : 'even' ?>"><td><?
echo "<a href=\"game.php?id=$gameID\">$name</a> - ".date('D jS M Y, G:i:s', $phpTimestamp)."</td></tr>";
9:16 am on Aug 11, 2010 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 18, 2008
votes: 0

$gameIDs = $game->getAttributeNode("game_id");
$gameID = $gameIDs->item(0)->nodeValue;

Yeah, that is incorrect as getAttributeNode returns a DOMAttr object [php.net...] which does not have an item() method.
If you really want to go by getAttributeNode, it would be:
$gameID = $game->getAttributeNode('game_id')->value;

However it is simpler to do:
$gameID = $game->getAttribute('game_id');


p.s. Welcome to webmasterworld!

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members