I am kinda new in XML but I can handle some things. Below there is a code that gets value from a HTML form and then displays some info from a XML file (see below). The code shows correctly all the info (TITLE, BAND, YEAR), but my question is how can I display also the id attribute of <CD id="XXX">. Thank you!
the code
<?php
$q=$_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("database.xml");
$x=$xmlDoc->getElementsByTagName('TITLE');
for ($i=0; $i<=$x->length-1; $i++)
{
//Process only element nodes
if ($x->item($i)->nodeType==1)
{
if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
{
$y=($x->item($i)->parentNode);
}
}
}
$cd=($y->childNodes);
for ($i=0;$i<$cd->length;$i++)
{
//Process only element nodes
if ($cd->item($i)->nodeType==1)
{
echo("<b>" . $cd->item($i)->nodeName . ":</b> ");
echo($cd->item($i)->childNodes->item(0)->nodeValue);
echo("<br />");
}
}
?>
XML
<CATEGORIES>
<CD id="1">
<TITLE>NEVER MIND THE BOLLOCKS</TITLE>
<BAND>SEX PISTOLS</BAND>
<YEAR>1977</YEAR>
</CD>
<CD id="2">
<TITLE>NEVERMIND</TITLE>
<BAND>NIRVANA</BAND>
<YEAR>1991</YEAR>
</CD>
</CATEGORIES>