简体   繁体   中英

PHP's XMLReader skips subtrees

I'm trying to parse a large XML file. It looks like:

<application>
  <action-keys>
    <case-file>...</case-file>
    <case-file>...</case-file>
    <case-file>...</case-file>
  </action-keys>
  <action-keys>
    <case-file>...</case-file>
    <case-file>...</case-file>
    <case-file>...</case-file>
  </action-keys>
<application>

I open it and seek the "case-file" values, but after the first set of case-files, doing a $xml->next("case-file") goes to the end of the file, the second set of case-files are skipped. How to get all the case-files?

You can try something like this. It will read all nodes of the files and extract text content of all case-file xml element.

$caseFiles = array();
$xml = new XMLReader();
$xml->open('content.xml');
while ($xml->read()) {
    if ($xml->nodeType == XMLReader::ELEMENT && $xml->name==='case-file') {
        $xml->read();
        $caseFiles[] = $xml->readString();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM