Possible Duplicate:
Printing content of a XML file using XML DOM
<?xml version="1.0" encoding="UTF-8"?>
<colors>
<apple>red</apple>
<banana>yellow</banana>
<grape>purple</grape>
</colors>
Since fruit names (tag names) are unknown, I'm trying to get all tag names and values with childNodes
of <colors>
$dom = new DOMDocument();
$dom->load("colors.xml");
$dom->preserveWhiteSpace = false;
$root = $dom->getElementsByTagName("colors")->item(0);
$colors = $root->childNodes;
echo "<p>".$colors->length."</p>";
foreach($colors as $color) {
echo "<p>".$color->nodeName." - ".$color->nodeValue."</p>";
}
$colors->length
returns 7 instead of 3, and the result:
#text -
apple - red
#text -
banana - yellow
#text -
grape - purple
#text -
Can someone please explain what are those #text -
? And what's wrong with my code? Thanks.
update
If I manually remove tabs and returns from the xml file, make the whole file into one line, #text -
are gone. But I don't think that's the way it should be.