Welcome to WebmasterWorld Guest from 54.198.46.95

Forum Moderators: httpwebwitch

Message Too Old, No Replies

Displaying attribute node. *Newbie Alert*

     
12:19 am on Aug 11, 2010 (gmt 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!

Rich

Excerpt:

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


XML:

<game game_id="game456">
<name>Swings</name>
<start_time>2010-08-29T11:00:00-05:00</start_time>
</game>


PHP:
<?
$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)

5+ Year Member



$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');


Enjoy!

p.s. Welcome to webmasterworld!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month