PHP DOM odd output?
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 =>
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";
}
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.
<?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...
精彩评论