tags:

views:

90

answers:

3

Hello everybody, I have XML file like this :

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

And i code in my php page:

<? php
$xmlDoc = new DOMDocument();
$xmlDoc->load("note.xml");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item)
{
   print $item->nodeName . " = " . $item->nodeValue . "<br />";
}
?>

The output of the code above is:

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

I known that when XML generates, it often contains white-spaces between the nodes. But Anybody can help me loop through all elements of the element of XML bypass white-spaces between the nodes. I mean loop through only all real elements of the element. Pls help me.

+4  A: 

Try this

$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false; 
$xmlDoc->load("note.xml");
roddik
Thanks you, thanks you so much. I'm editing my question but you answered it. Thanks
leduchuy89vn
+2  A: 

If you simply want to iterate over the elements (and do no fancy stuff like reordering elements and so on) I'd go for the simplexml-extension, which make dealing with XML much more simple:

$xml = simplexml_load_file('note.xml');
foreach ($xml as $node) {
    echo $node->getName() . ' = ' . (string)$node . '<br />';
}
Stefan Gehrig
A: 

Whatever you do, it will just be symptomatic treatment. The XML file should not contain those newline characters in the first place,as anything between the closing > and opening < is considered a text element.

Zed