Thread: Php xml dom
View Single Post
  #5  
Old 05-04-2009, 06:01 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 80,567
Looping through XML

We want to initialize the XML parser, load the XML, and loop through all elements of the element:
Example

$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml"); $x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
print $item->nodeName . " = " . $item->nodeValue . "
";
}
?>



The output of the code above will be:




#text =
to = Tove
#text =
from = Jani
#text =
heading = Reminder
#text =
body = Don't forget me this weekend!
#text =



In the example above you see that there are empty text nodes between each element.




When XML generates, it often contains white-spaces between the nodes. The XML DOM parser treats these as ordinary elements, and if you are not aware of them, they sometimes cause problems.
Reply With Quote