php-xml / large-text-node
处理大型文本节点需要使用低级PHP API函数。本包提供了一个简单接口,将大型文本节点写入资源,以将内存使用量降低到最小。
1.0.0
2016-07-23 10:33 UTC
Requires
- php: >=7.0
- ext-libxml: ^7.0
Requires (Dev)
- phpunit/phpunit: ^5.4
This package is not auto-updated.
Last update: 2024-09-26 02:06:02 UTC
README
PHP(例如SimpleXML)的高级XML API的缺点是它们将整个XML文档存储在内存中。因此,解析大型XML文件不是一件容易的事。
本包旨在以内存友好的方式读取大型文本节点,例如,可以将存储在XML文本节点中的base64编码文件写入文件。
选择文本节点
要读取文本节点的内容,必须提供一个路径。为了简化,路径只是一系列用正斜杠/
分隔的标签名称。目前,仅返回XML文档中的第一个匹配项。
例如,路径"document/tag/name"
将匹配I am matched
文本节点。
<document> <tag> <name>I am matched</name> </tag> <tag> <name>I am skipped</name> </tag> </document>
示例
为了给您一个概念,请参见以下示例
读取大型XML文件
<?php // php://temp writes first to memory, but if the text node is large, a temporary file is used on the fly. $output = fopen('php://temp', 'w+'); $input = fopen('some-large-file.xml', 'r'); $path = 'some/target'; // Use the resource reader to parse the XML document try { $reader = new StreamReader($input, $path, $output); $reader->parse(); rewind($output); // Maybe do something with $output } catch (XMLException $e) { // Whoops } finally { fclose($output); fclose($input); }
将base64编码的文本节点转换为文件
// Assume $input is some given stream $input = fopen('some-file'); $path = 'some/target'; // Open an output stream and add PHP's own base64 decoder filter $output = fopen('php://temp', 'w+'); stream_filter_append($output, 'convert.base64-decode'); try { $reader = new StreamReader($input, $path, $output); $reader->parse(); rewind($output); } catch (XMLException $e) { // Whoops } finally { fclose($output); fclose($input); }