首頁 >後端開發 >php教程 >如何用XMLReader读相同节点下的值?

如何用XMLReader读相同节点下的值?

WBOY
WBOY原創
2016-06-06 20:35:341148瀏覽

XML文件:

<code><human>
   <person>
        Jack
   </person>
   <person>
        Tom
   </person>
   <person>
        Jerry
   </person>
</human>

**代码:**

$reader = new XMLReader();
  $reader->open("example.xml");
    while ($reader->read()) {
      if($reader->nodeType==XMLREADER::ELEMENT) {
             if ($reader->localName == "Human") {
                    while ($reader->read()) {
                       if ($reader->nodeType == XMLREADER::ELEMENT) {
                                if ($reader->localName == "person") {
                                     $reader->read();
                                     echo $reader->value;

                                }
                                ...

while语句跳出需要执行break;但这里不知道从哪儿跳出,可以保证全部读出Tom,Jack,Jerry?
</code>

回复内容:

XML文件:

<code><human>
   <person>
        Jack
   </person>
   <person>
        Tom
   </person>
   <person>
        Jerry
   </person>
</human>

**代码:**

$reader = new XMLReader();
  $reader->open("example.xml");
    while ($reader->read()) {
      if($reader->nodeType==XMLREADER::ELEMENT) {
             if ($reader->localName == "Human") {
                    while ($reader->read()) {
                       if ($reader->nodeType == XMLREADER::ELEMENT) {
                                if ($reader->localName == "person") {
                                     $reader->read();
                                     echo $reader->value;

                                }
                                ...

while语句跳出需要执行break;但这里不知道从哪儿跳出,可以保证全部读出Tom,Jack,Jerry?
</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn