【发布时间】:2013-02-27 01:04:04
【问题描述】:
我一直在尝试使用 PHP 和 XMLReader 解析一个非常大的 XML 文件,但似乎无法获得我正在寻找的结果。基本上,我正在搜索大量信息,如果 a 包含某个邮政编码,我想返回那部分 XML,或者继续搜索直到找到该邮政编码。本质上,我会将这个大文件分解成几个小块,因此不必查看数千或数百万组信息,它可能是 10 或 20 组。
这里有一些我想要的 XML
//search through xml
<lineups country="USA">
//cache TX02217 as a variable
<headend headendId="TX02217">
//cache Grande Gables at The Terrace as a variable
<name>Grande Gables at The Terrace</name>
//cache Grande Communications as a variable
<mso msoId="17541">Grande Communications</mso>
<marketIds>
<marketId type="DMA">635</marketId>
</marketIds>
//check to see if any of the postal codes are equal to $pc variable that will be set in the php
<postalCodes>
<postalCode>11111</postalCode>
<postalCode>22222</postalCode>
<postalCode>33333</postalCode>
<postalCode>78746</postalCode>
</postalCodes>
//cache Austin to a variable
<location>Austin</location>
<lineup>
//cache all prgSvcID's to an array i.e. 20014, 10722
<station prgSvcId="20014">
//cache all channels to an array i.e. 002, 003
<chan effDate="2006-01-16" tier="1">002</chan>
</station>
<station prgSvcId="10722">
<chan effDate="2006-01-16" tier="1">003</chan>
</station>
</lineup>
<areasServed>
<area>
//cache community to a variable $community
<community>Thorndale</community>
<county code="45331" size="D">Milam</county>
//cache state to a variable i.e. TX
<state>TX</state>
</area>
<area>
<community>Thrall</community>
<county code="45491" size="B">Williamson</county>
<state>TX</state>
</area>
</areasServed>
</headend>
//if any of the postal codes matched $pc
//echo back the xml from <headend> to </headend>
//if none of the postal codes matched $pc
//clear variables and move to next <headend>
<headend>
etc
etc
etc
</headend>
<headend>
etc
etc
etc
</headend>
<headend>
etc
etc
etc
</headend>
</lineups>
PHP:
<?php
$pc = "78746";
$xmlfile="myFile.xml";
$reader = new XMLReader();
$reader->open($xmlfile);
while ($reader->read()) {
//search to see if groups contain $pc and echo info
}
我知道我做这件事的难度超出了应有的程度,但尝试操作如此大的文件时有点不知所措。任何帮助表示赞赏。
【问题讨论】:
-
您实际上在该 XML 块中寻找什么? XPath 是您的朋友。您只想查看是否有任何
包含预定值? -
有点。如果我搜索这个大文件,并且一个块包含一个预定的邮政编码,那么我基本上想返回那个块。它会将这个巨大文件的大小减少到 2%。我仍将返回 XML,但我必须参考的数量将大大减少。