【问题标题】:Large XML file Parsing Chunk data Filetering in PHP大型 XML 文件在 PHP 中解析块数据 Filetering
【发布时间】:2012-08-16 04:48:37
【问题描述】:

我有一个超过 100 MB 的大型 XML 文件。我正在像这样的块中读取文件

$fp = fopen('large.xml', 'r');

while ($data = fread($fp, 4096)) {

XML的格式是这样的

<PersonalInfo>
    <UserDetail>
       <FirstName>ABC</FirstName>
       <Occupation>Student</Occupation>
       <DateOfBirth>08/14/1999</DateOfBirth>
    </UserDetail>
    <CaseDetail>....</CaseDetail>
    <TransactionDetail>....</TransactionDetail>
</PersonalInfo>      
<PersonalInfo>
    <UserDetail>
       <FirstName>XYZ</FirstName>
       <Occupation>Student</Occupation>
       <DateOfBirth>04/25/1991</DateOfBirth>
     </UserDetail>
     <CaseDetail>....</CaseDetail>
     <TransactionDetail>.....</TransactionDetail>
</PersonalInfo>      
<PersonalInfo>
    <UserDetail>
        <FirstName>DEF</FirstName>
        <Occupation>Teacher</Occupation>
        <DateOfBirth>05/12/1984</DateOfBirth>
    </UserDetail>
    <CaseDetail>....</CaseDetail>
    <TransactionDetail>...</TransactionDetail>
</PersonalInfo>   

我只想包含职业标签为“学生”的那些记录,并将这些结果写入 CSV 文件。

我已尝试将 preg_match 作为 preg_match("/\(.*?)\/s", $data, $match); 选择标签,然后查看 $match 但它返回双值(重复)。

【问题讨论】:

标签: php xml regex xml-parsing


【解决方案1】:

首先通过以下链接检查您的 xml 是否有效:

http://www.xmlformatter.net/

如果您的 xml 有效,请执行以下操作:

$dom = new DOMDocument('1.0', 'UTF-8');

$dom->formatOutput = true;
@$dom->load('large.xml');
$tags = $dom->getElementsByTagName('PersonalInfo');

foreach ($tags as $destination) {

    foreach($destination->childNodes as $child) {
        if ($child->textContent == "Student") {
          echo "Write code to create csv file";
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-09
    • 2014-06-15
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    相关资源
    最近更新 更多