Below is my XML. I need to get each tag and format it in HTML. I am using PHP with foreach loops to do this as shown below:
<blog>
<title>H.M.S Endeaver</title>
<date>2011-06-03</date>
<body>text</body>
<tags>
<tag id="1">H.M.S Endeaver</tag>
<tag id="2">model-boat</tag>
</tags>
</blog>
$doc = new DOMDocument();
$doc->load($file);
$dataset = $doc->getElementsByTagName("blog");
foreach($dataset as $row)
{
$title = $row->getElementsByTagName("title")->item(0)->nodeValue;
$date = $row->getElementsByTagName("date")->item(0)->nodeValue;
$body = $row->getElementsByTagName("body")->item(0)->nodeValue;
$tags = $row->getElementsByTagName("tags");
.....
foreach($tags as $tag)
{
$tags = $tag->getElementsByTagName("tag")->item(0)->nodeValue;
echo "<a href='tag'>$tags</a>";
}
}
The problem is I dont know how to get all the different tag elements in the second foreach loop - would I use the attributes (id)?
Also is this the best approach to take i.e. could I have coded this better?
Thanks anyone