homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Displaying attribute node. *Newbie Alert*
froglet



 
Msg#: 4185124 posted 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>";
}
?>

 

astupidname

5+ Year Member



 
Msg#: 4185124 posted 9:16 am on Aug 11, 2010 (gmt 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');


Enjoy!

p.s. Welcome to webmasterworld!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved