Hi all
Here's a sample of some XML code:
<calendar>
<gig>
<when>
<day>Friday</day>
<date>5</date>
<month>August</month>
</when>
</gig>
<gig>
<when>
<day>Saturday</day>
<date>6</date>
<month>August</month>
</when>
</gig>
</calendar>
And here's the code I'm using to process the above and display the dates one at a time:
$xmlDoc = new DOMDocument();
$xmlDoc->load('test.xml');
$xmlGigs = $xmlDoc->getElementsByTagName("gig");
$i = 0;
foreach ($xmlGigs as $Gig)
{
$date = $Gig->getElementsByTagName("when");
echo "date = " . $date->item(0)->nodeValue . "<br/>";
++$i;
}
It works perfectly but, can someone tell me how it is that the full date (i.e. all three individual elements of "day", "date" and "month") gets easily displayed using the above example? I would have expected to have to extract and display each element at a time and was surprised to find the above worked...
Secondly, how can I parse just one of those elements? I want to add a second line to the above FOREACH example to display just the "day" but the only way I can find of doing it is like this:
echo "day = " . $Gig->getElementsByTagName("day")->item(0)->nodeValue . "<br/>";
I actually don't want to use the getElementsByTagName("day") method and instead want to extract the info from the $date variable I've already created using something like:
echo "day = " . $date->childNode(0)->item(0)->nodeValue . "<br/>";
..but just can't get anything to work. I'm betting its something so simple it's embarrassing but can anyone help?