【发布时间】:2017-01-25 00:34:47
【问题描述】:
谁能告诉我为什么下面的第一行可以工作,而其他 4 行不行?
对于以下 5 行中的每一行...
1.) 右边----我写下了发生的事情。
2.)在下面——我写了我期望发生的事情。
PHP
$doc = new DOMDocument();
$doc->load($str);
$doc->preserveWhiteSpace = true;
$doc->formatOutput = true;
/*1*/ echo $doc->firstChild->nodeValue; //WORKED - Echoed the whole DOC
//doc-> comments
/*2*/ echo $doc->firstChild->firstChild->nodeValue; //DIDNT WORK
//doc-> comments -> post
/*3*/ echo $doc->firstChild->firstChild->textContent; //DIDNT WORK
//doc-> comments -> post
/*4*/ echo $doc->firstChild->firstChild->nextSibling->nodeValue; //Echoed whole 1st <post>
//doc-> comments -> post -> 2nd post
/*5*/ echo $doc->firstChild->firstChild->nextSibling->firstChild->nodeValue; //Echoed 1st <post>'s <id>("1").
//doc-> comments -> post -> 2nd post -> id ("2")
XML
<?xml version="1.0"?>
<comments>
<post>
<id>1</id><author>Demetrius</author>
</post>
<post>
<id>2</id><author>Demetrius</author>
</post>
</comments>
我能想出的唯一解释是我有“抵消错误”,所以 (在树的各个级别)...
2.) firstChild 确实是<?xml version="1.0"?> 标签,并且
3.) firstChild 充当nextSibling 然后
4.) nextSibling 充当firstChild。
但这没有任何意义。
【问题讨论】: