Printing content of a XML file using XML DOM

Explanation for weird #text strings

The weird #text strings dont come out of the blue but are actual Text Nodes. When you load a formatted XML document with DOM any whitespace, e.g. indenting, linebreaks and node values will be part of the DOM as DOMText instances by default, e.g.

<cellphones>\n\t<telefon>\n\t\t<model>Easy DB…
E           T   E        T     E      T      

where E is a DOMElement and T is a DOMText.

To get around that, load the document like this:

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->load('file.xml');

Then your document will be structured as follows

<cellphones><telefon><model>Easy DB…
E           E        E      T

Note that individual nodes representing the value of a DOMElement will still be DOMText instances, but the nodes that control the formatting are gone. More on that later.

Proof

You can test this easily with this code:

$dom = new DOMDocument;
$dom->preserveWhiteSpace = TRUE; // change to FALSE to see the difference
$dom->load('file.xml');
foreach ($dom->getElementsByTagName('telefon') as $telefon) {
    foreach($telefon->childNodes as $node) {
        printf(
            "Name: %s - Type: %s - Value: %s\n",
            $node->nodeName,
            $node->nodeType,
            urlencode($node->nodeValue)
        );
    }
}

This code runs through all the telefon elements in your given XML and prints out node name, type and the urlencoded node value of it’s child nodes. When you preserve the whitespace, you will get something like

Name: #text - Type: 3 - Value: %0A++++
Name: model - Type: 1 - Value: Easy+DB
Name: #text - Type: 3 - Value: %0A++++
Name: proizvodjac - Type: 1 - Value: Alcatel
Name: #text - Type: 3 - Value: %0A++++
Name: cena - Type: 1 - Value: 25
Name: #text - Type: 3 - Value: %0A++
…

The reason I urlencoded the value is to show that there is in fact DOMText nodes containing the indenting and the linebreaks in your DOMDocument. %0A is a linebreak, while each + is a space.

When you compare this with your XML, you will see there is a line break after each <telefon> element followed by four spaces until the <model> element starts. Likewise, there is only a newline and two spaces between the closing <cena> and the opening <telefon>.

The given type for these nodes is 3, which – according to the list of predefined constants – is XML_TEXT_NODE, e.g. a DOMText node. In lack of a proper element name, these nodes have a name of #text.

Disregarding Whitespace

Now, when you disable preservation of whitespace, the above will output:

Name: model - Type: 1 - Value: Easy+DB
Name: proizvodjac - Type: 1 - Value: Alcatel
Name: cena - Type: 1 - Value: 25
Name: model - Type: 1 - Value: 3310
…

As you can see, there is no more #text nodes, but only type 1 nodes, which means XML_ELEMENT_NODE, e.g. DOMElement.

DOMElements contain DOMText nodes

In the beginning I said, the values of DOMElements are DOMText instances too. But in the output above, they are nowhere to be seen. That’s because we are accessing the nodeValue property, which returns the value of the DOMText as string. We can prove that the value is a DOMText easily though:

$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
foreach ($dom->getElementsByTagName('telefon') as $telefon) {
    $node = $telefon->firstChild->firstChild; // 1st child of model
    printf(
        "Name: %s - Type: %s - Value: %s\n",
        $node->nodeName,
        $node->nodeType,
        urlencode($node->nodeValue)
    );
}

will output

Name: #text - Type: 3 - Value: Easy+DB
Name: #text - Type: 3 - Value: 3310
Name: #text - Type: 3 - Value: GF768
Name: #text - Type: 3 - Value: Skeleton
Name: #text - Type: 3 - Value: Earl

And this proves a DOMElement contains it’s value as a DOMText and nodeValue is just returning the content of the DOMText directly.

More on nodeValue

In fact, nodeValue is smart enough to concatenate the contents of any DOMText children:

$dom = new DOMDocument;
$dom->loadXML('<root><p>Hello <em>World</em>!!!</p></root>');
$node = $dom->documentElement->firstChild; // p
printf(
    "Name: %s - Type: %s - Value: %s\n",
    $node->nodeName,
    $node->nodeType,
    $node->nodeValue
);

will output

Name: p - Type: 1 - Value: Hello World!!!

although these are really the combined values of

DOMText "Hello"
DOMElement em with DOMText "World"
DOMText "!!!"

Printing content of a XML file using XML DOM

To finally answer your question, look at the first test code. Everything you need is in there. And of course by now you have been given fine other answers too.

Leave a Comment