我一直試圖用PHP和XMLReader解析一個非常大的XML文件,但似乎無法得到我期待的結果。基本上,我正在搜索大量的信息,如果某個郵件包含某個郵政編碼,我想返回那一點XML,或者繼續搜索,直到找到該郵政編碼。從本質上講,我將把這個大文件分解成幾個小塊,所以不必查看數千或數百萬個信息組,它可能是10或20個。使用PHP和XMLReader解析XML
這裏有一個位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是你的朋友。你只是想看看是否有包含預定值? –
mkaatman
2013-03-11 18:15:28
類別。如果我搜索這個大文件,並且塊包含預定的郵編,那麼我想基本上返回該塊。它會將這個龐大文件的大小減少到2%。我仍然會返回XML,但是我將不得不引用的數量將會大大減小。 – user1129107 2013-03-11 18:21:14