Display attribute from XML using PHP
- by user560411
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>