Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Explanation for weird #text strings</strong></p> <p>The weird #text strings dont come out of the blue but are actual Text Nodes. When you load a formatted XML document with <code>DOM</code> any whitespace, e.g. indenting, linebreaks and node values will be part of the DOM as <code>DOMText</code> instances by default, e.g.</p> <pre><code>&lt;cellphones&gt;\n\t&lt;telefon&gt;\n\t\t&lt;model&gt;Easy DB… E T E T E T </code></pre> <p>where E is a <code>DOMElement</code> and T is a <code>DOMText</code>.</p> <p>To get around that, load the document like this:</p> <pre><code>$dom = new DOMDocument; $dom-&gt;preserveWhiteSpace = FALSE; $dom-&gt;load('file.xml'); </code></pre> <p>Then your document will be structured as follows</p> <pre><code>&lt;cellphones&gt;&lt;telefon&gt;&lt;model&gt;Easy DB… E E E T </code></pre> <p>Note that individual nodes representing the value of a <code>DOMElement</code> will still be <code>DOMText</code> instances, but the nodes that control the formatting are gone. More on that later.</p> <p><strong>Proof</strong></p> <p>You can test this easily with this code:</p> <pre><code>$dom = new DOMDocument; $dom-&gt;preserveWhiteSpace = TRUE; // change to FALSE to see the difference $dom-&gt;load('file.xml'); foreach ($dom-&gt;getElementsByTagName('telefon') as $telefon) { foreach($telefon-&gt;childNodes as $node) { printf( "Name: %s - Type: %s - Value: %s\n", $node-&gt;nodeName, $node-&gt;nodeType, urlencode($node-&gt;nodeValue) ); } } </code></pre> <p>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</p> <pre><code>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++ … </code></pre> <p>The reason I urlencoded the value is to show that there is in fact <code>DOMText</code> nodes containing the indenting and the linebreaks in your <code>DOMDocument</code>. <code>%0A</code> is a linebreak, while each <code>+</code> is a space. </p> <p>When you compare this with your XML, you will see there is a line break after each <code>&lt;telefon&gt;</code> element followed by four spaces until the <code>&lt;model&gt;</code> element starts. Likewise, there is only a newline and two spaces between the closing <code>&lt;cena&gt;</code> and the opening <code>&lt;telefon&gt;</code>. </p> <p>The given type for these nodes is 3, which - <a href="http://de.php.net/manual/en/dom.constants.php" rel="nofollow">according to the list of predefined constants</a> - is <code>XML_TEXT_NODE</code>, e.g. a <code>DOMText</code> node. In lack of a proper element name, these nodes have a name of #text.</p> <p><strong>Disregarding Whitespace</strong></p> <p>Now, when you disable preservation of whitespace, the above will output:</p> <pre><code>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 … </code></pre> <p>As you can see, there is no more #text nodes, but only type 1 nodes, which means <code>XML_ELEMENT_NODE</code>, e.g. <code>DOMElement</code>. </p> <p><strong>DOMElements contain DOMText nodes</strong></p> <p>In the beginning I said, the values of <code>DOMElements</code> are <code>DOMText</code> instances too. But in the output above, they are nowhere to be seen. That's because we are accessing the <a href="http://de.php.net/manual/en/class.domnode.php#domnode.props.nodevalue" rel="nofollow"><code>nodeValue</code></a> property, which returns the value of the <code>DOMText</code> as string. We can prove that the value is a <code>DOMText</code> easily though:</p> <pre><code>$dom = new DOMDocument; $dom-&gt;preserveWhiteSpace = FALSE; $dom-&gt;loadXML($xml); foreach ($dom-&gt;getElementsByTagName('telefon') as $telefon) { $node = $telefon-&gt;firstChild-&gt;firstChild; // 1st child of model printf( "Name: %s - Type: %s - Value: %s\n", $node-&gt;nodeName, $node-&gt;nodeType, urlencode($node-&gt;nodeValue) ); } </code></pre> <p>will output</p> <pre><code>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 </code></pre> <p>And this proves a <code>DOMElement</code> contains it's value as a <code>DOMText</code> and <code>nodeValue</code> is just returning the content of the <code>DOMText</code> directly. </p> <p><strong>More on nodeValue</strong></p> <p>In fact, <code>nodeValue</code> is smart enough to concatenate the contents of any <code>DOMText</code> children:</p> <pre><code>$dom = new DOMDocument; $dom-&gt;loadXML('&lt;root&gt;&lt;p&gt;Hello &lt;em&gt;World&lt;/em&gt;!!!&lt;/p&gt;&lt;/root&gt;'); $node = $dom-&gt;documentElement-&gt;firstChild; // p printf( "Name: %s - Type: %s - Value: %s\n", $node-&gt;nodeName, $node-&gt;nodeType, $node-&gt;nodeValue ); </code></pre> <p>will output</p> <pre><code>Name: p - Type: 1 - Value: Hello World!!! </code></pre> <p>although these are really the combined values of</p> <pre><code>DOMText "Hello" DOMElement em with DOMText "World" DOMText "!!!" </code></pre> <p><strong>Printing content of a XML file using XML DOM</strong></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload