Display attribute from XML using PHP
Posted
by
user560411
on Stack Overflow
See other posts from Stack Overflow
or by user560411
Published on 2011-01-02T17:42:34Z
Indexed on
2011/01/02
17:53 UTC
Read the original article
Hit count: 173
Hello. I need to display the id attribute of CD from the following XML file. I display correctly everything except the id. Any help would be appreciate.
display code
<?php
$doc = new DOMDocument();
$doc->load( 'insert.xml' );
$CATEGORIES = $doc->getElementsByTagName( "CD" );
foreach( $CATEGORIES as $CD )
{
$TITLES = $CD->getElementsByTagName( "TITLE" );
$TITLE = $TITLES->item(0)->nodeValue;
$BANDS= $CD->getElementsByTagName( "BAND" );
$BAND= $BANDS->item(0)->nodeValue;
$YEARS = $CD->getElementsByTagName( "YEAR" );
$YEAR = $YEARS->item(0)->nodeValue;
echo "<b>$TITLE - $BAND - $YEAR\n</b><br>";
}
?>
XML
<?xml version="1.0" encoding="utf-8"?>
<MY_CD>
<CATEGORIES>
<CD id="3231">
<TITLE>NEVER MIND THE BOLLOCKS</TITLE>
<BAND>SEX PISTOLS</BAND>
<YEAR>1977</YEAR>
</CD>
<CD id="2453">
<TITLE>NEVERMIND</TITLE>
<BAND>NIRVANA</BAND>
<YEAR>1991</YEAR>
</CD>
</CATEGORIES>
</MY_CD>
© Stack Overflow or respective owner