流解析 4GB XML 文件,并将部分文件写入 PHP 中的新 XML 文件


Stream parse 4GB XML file and write part of file to new XML file in PHP

我正在尝试流式解析一个大约 4GB 的 XML 文件并将其部分写入 PHP 中的新 XML 文件。

~4GB XML文档的结构是这样的,我试图保留<doc>元素及其<title></title> <url></url><abstract></abstract>子元素。

但是当我运行这个脚本时,我得到的只是一个文件,每行都有一个<doc />文件。所以基本上它是复制<doc>元素并使它们自闭合,而不是复制它的子元素。

<?php
    $interestingNodes = array('title','url','abstract');
    $xmlObject = new XMLReader();
    $xmlObject->open('file.xml');
    $xmlOutput = new XMLWriter();
    $xmlOutput->openURI('destfile.xml');
    $xmlOutput->setIndent(true);
    $xmlOutput->setIndentString("   ");
    $xmlOutput->startDocument('1.0', 'UTF-8');
    while($xmlObject->read()){
        if($xmlObject->name == 'doc'){
             $xmlOutput->startElement('doc');
             $xmlObject->readInnerXML();
             if(array_search($xmlObject->name, $interestingNodes)){
                 $xmlOutput->startElement($xmlObject->name);
                 $xmlOutput->text($xmlObject->value);
                 $xmlOutput->endElement(); //close the current node
             }
             $xmlOutput->endElement(); //close the doc node
        }
    }
    $xmlObject->close();
    $xmlOutput->endDocument();
    $xmlOutput->flush();
?>

文件.xml如下所示:

<feed>
    <doc>
        <title>Title of first doc is here</title>
        <url>URL is here</url>
        <abstract>Abstract is here...</abstract>
        <links>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
       </link>
    </doc>
    <doc>
        <title>Title of second doc is here</title>
        <url>URL is here</url>
        <abstract>Abstract is here...</abstract>
        <links>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
            <sublink>Link is here</sublink>
        </link>
    </doc>
 </feed>

这就是我想要的 destfile.xml 的样子:

    <doc>
        <title>Title of first doc is here</title>
        <url>URL is here</url>
        <abstract>Abstract is here...</abstract>
    </doc>
    <doc>
        <title>Title of second doc is here</title>
        <url>URL is here</url>
        <abstract>Abstract is here...</abstract>
    </doc>

但是当我运行上面的脚本时,我得到的只是:

<doc />
<doc />
<doc />
<doc />
<doc />
<doc />
/* And many, many more <doc />s */
我相信

以下内容可以完成您正在尝试做的事情:

<?php
    $interestingNodes = array('title','url','abstract');
    $xmlObject = new XMLReader();
    $xmlObject->open('file.xml');
    $xmlOutput = new XMLWriter();
    $xmlOutput->openURI('destfile.xml');
    $xmlOutput->setIndent(true);
    $xmlOutput->setIndentString("   ");
    $xmlOutput->startDocument('1.0', 'UTF-8');
    while($xmlObject->read()){
        if($xmlObject->name == 'doc'){
            if($xmlObject->nodeType==XMLReader::END_ELEMENT) $xmlOutput->endElement();
            else $xmlOutput->startElement('doc');
        }
        if(in_array($xmlObject->name, $interestingNodes)){
            $xmlOutput->startElement($xmlObject->name);
            $xmlOutput->text($xmlObject->readString());
            $xmlOutput->endElement(); //close the current node
        }
    }
    $xmlObject->close();
    $xmlOutput->endDocument();
    $xmlOutput->flush();
?>