php 大文件xml,在PHP中解析巨大的XML文件

只有两个php API真的适合处理大文件。第一个是旧的

expat api,第二个是较新的

XMLreader函数。这些apis读取连续流,而不是将整个树加载到内存(这是simplexml和DOM的作用)。

例如,您可能要查看DMOZ目录的这个部分解析器:

class SimpleDMOZParser

{

protected $_stack = array();

protected $_file = "";

protected $_parser = null;

protected $_currentId = "";

protected $_current = "";

public function __construct($file)

{

$this->_file = $file;

$this->_parser = xml_parser_create("UTF-8");

xml_set_object($this->_parser, $this);

xml_set_element_handler($this->_parser, "startTag", "endTag");

}

public function startTag($parser, $name, $attribs)

{

array_push($this->_stack, $this->_current);

if ($name == "TOPIC" && count($attribs)) {

$this->_currentId = $attribs["R:ID"];

}

if ($name == "LINK" && strpos($this->_currentId, "Top/Home/Consumer_Information/Electronics/") === 0) {

echo $attribs["R:RESOURCE"] . "\n";

}

$this->_current = $name;

}

public function endTag($parser, $name)

{

$this->_current = array_pop($this->_stack);

}

public function parse()

{

$fh = fopen($this->_file, "r");

if (!$fh) {

die("Epic fail!\n");

}

while (!feof($fh)) {

$data = fread($fh, 4096);

xml_parse($this->_parser, $data, feof($fh));

}

}

}

$parser = new SimpleDMOZParser("content.rdf.u8");

$parser->parse();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值