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

流解析4GB XML文件,并将部分文件写入PHP中的新XML文件,php,xml,xml-parsing,large-data,Php,Xml,Xml Parsing,Large Data,我正在尝试对一个大约4GB的XML文件进行流解析,并用PHP将其中的一部分写入一个新的XML文件 ~4GB XML文档的结构如下所示,我试图保留元素及其和子元素。 但是当我运行这个脚本时,我得到的只是一个文件,每行有一个。所以基本上它是复制元素并使它们自动关闭,而不是复制它的子元素 <?php $interestingNodes = array('title','url','abstract'); $xmlObject = new XMLReader(); $xm

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

~4GB XML文档的结构如下所示,我试图保留
元素及其
子元素。

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

<?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();

?>

下面是file.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>

第一份文件的标题在这里
URL在这里
摘要在这里。。。
链接在这里
链接在这里
链接在这里
链接在这里
链接在这里
第二份文件的标题在这里
URL在这里
摘要在这里。。。
链接在这里
链接在这里
链接在这里
链接在这里
链接在这里
这就是我想要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>

第一份文件的标题在这里
URL在这里
摘要在这里。。。
第二份文件的标题在这里
URL在这里
摘要在这里。。。
但当我运行该脚本时,首先,我得到的是:

<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();

?>

数组搜索
需要
==false
检查,否则您将永远无法获得“标题”(位置0)