百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
X

xml-string-streamer

> 编程语言
开源

以低内存消耗的方式流式传输大型 XML 文件。

360 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

以低内存消耗的方式流式传输大型 XML 文件。

xml-string-streamer

Purpose

To stream XML files too big to fit into memory, with very low memory consumption. This library is a successor to XmlStreamer.

Installation

Legacy support

  • All versions below 1 support PHP 5.3 - 7.2
  • Version 1 and above support PHP 7.2+

With composer

Run composer require prewk/xml-string-streamer to install this package.

Usage

Let's say you have a 2 GB XML file gigantic.xml containing customer items that look like this:

xml

    
        Jane
        Doe
    
    ...

Create a streamer and parse it:

php
// Convenience method for creating a file streamer with the default parser
$streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml");

while ($node = $streamer->getNode()) {
    // $node will be a string like this: "JaneDoe"
    $simpleXmlNode = simplexml_load_string($node);
    echo (string)$simpleXmlNode->firstName;
}

Without the convenience method (functionally equivalient):

php
use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Stream;
use Prewk\XmlStringStreamer\Parser;

// Prepare our stream to be read with a 1kb buffer
$stream = new Stream\File("gigantic.xml", 1024);

// Construct the default parser (StringWalker)
$parser = new Parser\StringWalker();

// Create the streamer
$streamer = new XmlStringStreamer($parser, $stream);

// Iterate through the `` nodes
while ($node = $streamer->getNode()) {
    // $node will be a string like this: "JaneDoe"
    $simpleXmlNode = simplexml_load_string($node);
    echo (string)$simpleXmlNode->firstName;
}

Convenience method for the UniqueNode parser:

php
$streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));

Parsers

Parser\StringWalker

Works like an XmlReader, and walks the XML tree node by node. Captures by node depth setting.

Parser\UniqueNode

A much faster parser that captures everything between a provided element's opening and closing tags. Special prerequisites apply.

Stream providers

Stream\File

Use this provider to parse large XML files on disk. Pick a chunk size, for example: 1024 bytes.

php
$CHUNK_SIZE = 1024;
$provider = new Prewk\XmlStringStreamer\Stream\File("large-xml-file.xml", $CHUNK_SIZE);

Stream\Stdin

Use this provider if you want to create a CLI application that streams large XML files through STDIN.

php
$CHUNK_SIZE = 1024;
$fsp = new Prewk\XmlStringStreamer\Stream\Stdin($CHUNK_SIZE);

Stream\Guzzle

Use this provider if you want to stream over HTTP with Guzzle. Resides in its own repo due to its higher PHP version requirements (5.5): https://github.com/prewk/xml-string-streamer-guzzle

StringWalker Options

Usage

php
use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Parser;
use Prewk\XmlStringStreamer\Stream;

$options = array(
    "captureDepth" => 3
);

$parser = new Parser\StringWalker($options);

Available options for the StringWalker parser

Option Default Description
(int) captureDepth 2 Depth we start collecting nodes at
(array) tags See example Supported tags
(bool) expectGT false Whether to support > in XML comments/CDATA or not
(array) tagsWithAllowedGT See example If expectGT is true, this option lists the tags with allowed > characters in them

Examples

captureDepth

Default behavior with a capture depth of 2:

xml

    
        ...
    
    
        ...
    

..will capture the `` nodes.

But say your XML looks like this:

xml

    
        
            ...
        
        
            ...
        
    

Then you'll need to set the capture depth to 3 to capture the `` nodes.

Node depth visualized:

xml
 
 
     
         
        
    

tags

Default value:

php
array(
    array("", 0),
    array("", 0),
    array("", 0),
    array("", 0),
    array("", -1),
    array("", 0),
    array("", 1)
),

First parameter: opening tag, second parameter: closing tag, third parameter: depth.

If you know that your XML doesn't have any XML comments, CDATA or self-closing tags, you can tune your performance by setting the tags option and omitting them:

php
array(
    array("", 0),
    array("", 0),
    array("", -1),
    array("", 1)
),

expectGT & tagsWithAllowedGT

You can allow the > character within XML comments and CDATA sections if you want. This is pretty uncommon, and therefore turned off by default for performance reasons.

Default value for tagsWithAllowedGT:

php
array(
    array(""),
    array("")
),

UniqueNode Options

Usage

php
use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Parser;
use Prewk\XmlStringStreamer\Stream;

$options = array(
    "uniqueNode" => "TheNodeToCapture"
);

$parser = new Parser\UniqueNode($options);

Available options for the UniqueNode parser

Option Description
(string) uniqueNode Required option: Specify the node name to capture
(bool) checkShortClosing Whether to check short closing tag or not

Examples

uniqueNode

Say you have an XML file like this:

xml

    
        ...
    
    
        ...
    
    
        ...
    

You want to capture the stuff nodes, therefore set uniqueNode to "stuff".

If you have an XML file with short closing tags like this:

xml

    
    
        ...
    
    

You want to capture the stuff nodes, therefore set uniqueNode to "stuff" and checkShortClosing to true.

But if your XML file look like this:

xml

    
        Lorem ipsum
        
            Oops, another stuff node
        
    
    ...

..you won't be able to use the UniqueNode parser, because exists inside of another node.

Advanced Usage

Progress bar

You can track progress using a closure as the third argument when constructing the stream class. Example with the File stream using the StringWalker parser:

…

`

You could of course do something more intelligent than spamming with echo.

Accessing the root element (version 0.7.0+)

Setting the parser option extractContainer tells the parser to gather everything before and after your intended child element capture. The results are available via the parser's getExtractedContainer() method.

Note: getExtractedContainer() will return different things depending on if you've streamed the whole file or not. If you need the containing XML data prematurely you can get it inside of the while loop, but it will just be the opening elements and therefore considered invalid XML by parsers such as SimpleXML.

…

`

This method should be considered experimental, and may extract weird stuff in edge cases

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言