我一直在尝试使用 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
}
我知道我正在使这比它应该做的更难,但我有点不知所措试图操纵这么大的文件。感谢您的帮助。
最佳答案
通过 XMLReader
获得更大的灵 active 我通常自己创建 iterators that are able to work on the XMLReader
object and provide the steps I need .
从对所有节点的简单迭代开始,到对具有特定名称的可选元素的迭代。我们叫最后一个XMLElementIterator
将阅读器和元素名称作为参数。
在您的场景中,我将创建一个返回 SimpleXMLElement
的迭代器对于当前元素,只取 <headend>
元素:
require('xmlreader-iterators.php'); // https://gist.github.com/hakre/5147685
class HeadendIterator extends XMLElementIterator {
const ELEMENT_NAME = 'headend';
public function __construct(XMLReader $reader) {
parent::__construct($reader, self::ELEMENT_NAME);
}
/**
* @return SimpleXMLElement
*/
public function current() {
return simplexml_load_string($this->reader->readOuterXml());
}
}
配备了这个迭代器,您剩下的工作基本上是小菜一碟。首先加载 10 GB 的文件:
$pc = "78746";
$xmlfile = '../data/lineups.xml';
$reader = new XMLReader();
$reader->open($xmlfile);
然后检查 <headend>
元素包含信息,如果是,则显示数据/XML:
foreach (new HeadendIterator($reader) as $headend) {
/* @var $headend SimpleXMLElement */
if (!$headend->xpath("/*/postalCodes/postalCode[. = '$pc']")) {
continue;
}
echo 'Found, name: ', $headend->name, "\n";
echo "==========================================\n";
$headend->asXML('php://stdout');
}
这确实实现了您想要实现的目标:迭代大型文档(内存友好)直到找到您感兴趣的元素。然后处理具体元素,它是 XML只要; XMLReader::readOuterXml()
在这里是一个很好的工具。
示例输出:
Found, name: Grande Gables at The Terrace
==========================================
<?xml version="1.0"?>
<headend headendId="TX02217">
<name>Grande Gables at The Terrace</name>
<mso msoId="17541">Grande Communications</mso>
<marketIds>
<marketId type="DMA">635</marketId>
</marketIds>
<postalCodes>
<postalCode>11111</postalCode>
<postalCode>22222</postalCode>
<postalCode>33333</postalCode>
<postalCode>78746</postalCode>
</postalCodes>
<location>Austin</location>
<lineup>
<station prgSvcId="20014">
<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>
<community>Thorndale</community>
<county code="45331" size="D">Milam</county>
<state>TX</state>
</area>
<area>
<community>Thrall</community>
<county code="45491" size="B">Williamson</county>
<state>TX</state>
</area>
</areasServed>
</headend>
关于php - 使用 PHP 和 XMLReader 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15345615/