0

Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<columns>
    <column>
        <originalName>ORP</originalName>
        <name>ORP</name>
        <visible>1</visible>
    </column>
    <column>
        <originalName>OKRES</originalName>
        <name>OKRES</name>
        <visible>1</visible>
    </column>
    <column>
        <originalName>Objekt</originalName>
        <name>Objekt</name>
        <visible>1</visible>
    </column>
    <column>
        <originalName>Školenie</originalName>
        <name>Skolenie</name>
        <visible>1</visible>
    </column>
    <column>
        <originalName>Dátum posledného školenia OPP</originalName>
        <name>DatumPoslednehoSkoleniaOPP</name>
        <visible>1</visible>
    </column>
    <column>
        <originalName>Dátum posledného školenia OPP</originalName>
        <name>DatumPoslednehoSkoleniaOPP1</name>
        <visible>0</visible>
    </column>
</columns>

My code to iterate over all columns:

$schema = new DOMDocument();
$schema->load(BASE_PATH.'/schema.xml');
foreach ($schema->getElementsByTagName('column') as $column) {
    foreach($column->childNodes as $child) {
        echo $child->nodeName, ' => ', $child->nodeValue, '<br />';
    }
}

Now the output is most odd. I don't understand why the '#text =>' keeps appearing there:

#text =>
originalName => ORP
#text =>
name => ORP
#text =>
visible => 1
#text =>
#text =>
originalName => OKRES
#text =>
name => OKRES
#text =>
visible => 1
#text =>
#text =>
originalName => Objekt
#text =>
name => Objekt
#text =>
visible => 1
#text =>
#text =>
originalName => Školenie
#text =>
name => Skolenie
#text =>
visible => 1
#text =>
#text =>
originalName => Dátum posledného školenia OPP
#text =>
name => DatumPoslednehoSkoleniaOPP
#text =>
visible => 1
#text =>
#text =>
originalName => Dátum posledného školenia OPP
#text =>
name => DatumPoslednehoSkoleniaOPP1
#text =>
visible => 0
#text =>

3 Answers 3

2

You could skip the DOMText elements:

$schema = new DOMDocument();
$schema->load(BASE_PATH.'/schema.xml');
foreach ($schema->getElementsByTagName('column') as $column) {
    foreach($column->childNodes as $child) {
        if ($child instanceof DOMElement) { // (! $child instanceof DOMText)
            echo $child->nodeName, ' => ', $child->nodeValue, '<br />';
        }
    }
}

Or use DOMXPath:

$doc = new DOMDocument();
$doc->load(BASE_PATH.'/schema.xml');
$xpath = new DOMXPath($doc);

foreach($xpath->query('/columns/column/*') as $child)
{
    echo $child->nodeName, ' => ', $child->nodeValue, "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

2

Did you try and load() with the LIBXML_NOBLANKS option?

Alternatively loadXML() might do what you want (I'm guessing here).


According to PHP, DOM and XML : Part 2 – DOMDocument you should either:

$schema->load(BASE_PATH.'/schema.xml', LIBXML_NOBLANKS);

Or

$schema = new DOMDocument();
$schema->preserveWhiteSpace = false; // This is the important part
$schema->load(BASE_PATH.'/schema.xml');

Depending on your PHP version.

Comments

1
<?php
$schema = new DOMDocument();
$schema->load('schema.xml');
foreach ($schema->getElementsByTagName('column') as $column) {
    $array = false;$i=0;
    foreach ($column->childNodes as $childNode){
        $array[$i] = $childNode->firstChild->nodeValue;
        $i++;
    }
    echo $array[1]." = > ".$array[3]." = > ".$array[5]."<br>";
}
?>

try this one... its not by falowing standards and bla bla bla or something like that but it works! :P

p.s. here you have some explanations/tutorials if you want read...

3 Comments

Take a look at my schema, there is no '#text' element there whatsoever. It should not get printed.
try with this print_r($child); and then you will se that there is #text :) (DOMText Object ( ) originalName => ORP), (DOMElement Object ( ) #text => )
use echo $child->nodeValue."<br>"; and you will get it clean... dont know for what you need it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.