欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-06-13 21:36:24
...
XML文件:

        Jack
   
        Tom
   
        Jerry
   

**代码:**

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

回复内容:

XML文件:


        Jack
   
        Tom
   
        Jerry
   

**代码:**

$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?
相关标签: php xml xmldom