Home > Backend Development > PHP Tutorial > 如何用XMLReader读相同节点下的值?

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

WBOY
Release: 2016-06-06 20:35:34
Original
1151 people have browsed it

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>
Copy after login
Copy after login

回复内容:

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>
Copy after login
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template