2011-01-27 40 views
0

這裏是我的XML:PHP DOM奇數輸出?

<?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> 

我的代碼遍歷所有列:

$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 />'; 
    } 
} 

現在輸出是最奇怪的。我不明白爲什麼「#text =>」繼續出現有:

#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 => 

回答

2

你可以跳過一個DOMText元素:

$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 />'; 
     } 
    } 
} 

或者使用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"; 
} 
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>"; 
} 
?> 

試試這個...它不是由falowing標準和唧唧歪歪或者類似的東西,但它的工程! :P

p.s. here你有一些解釋/教程,如果你想閱讀...

+0

看看我的架構,不存在 '#text' 元素有任何責任。它不應該打印。 – 2011-01-27 08:58:19

+0

試試這個print_r($ child); (DOMText Object()originalName => ORP),(DOMElement Object()#text =>) – FeRtoll 2011-01-27 09:01:22

2

你試過load()LIBXML_NOBLANKS選項?

或者loadXML()可能會做你想做的(我在這裏猜測)。


根據PHP, DOM and XML : Part 2 – DOMDocument你應該:

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

或者

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

根據您的PHP版本。