runopencode/sax
Composer 安装命令:
composer require runopencode/sax
包简介
XML SAX (JAVA like) parser
关键字:
README 文档
README
This library enables you to parse XML documents with SAX in Java style: instead of handling events by using these nasty
functions and callbacks (see official PHP documentation example here),
you can just inherit provided abstract class RunOpenCode\Sax\Handler\AbstractSaxHandler and implement all of its abstract
methods.
Major benefit of using this library is clean, human-readable code.
Example:
class MySaxHandler extends RunOpenCode\Sax\Handler\AbstractSaxHandler {
// ... your implementation
}
$result = RunOpenCode\Sax\SaxParser::factory()->parse(new MySaxHandler(), $myXmlDocumentResource);
List of methods which you ought to implement:
onDocumentStart: executed when parsing started of XML document.onElementStart: executed when parser stumbled upon new XML tag.onElementData: executed when parser stumbled upon CDATA of some XML tag.onElementEnd: executed when parser stumbled upon closed already opened XML tag.onDocumentEnd: executed when parsing of XML document is done.onParseError: executed when parsing error is triggered.getResult: executed at very end of parsing process where you should provide a invoker with parsing results.
Since common usage of Sax parser is to have a stack of currently working elements,
there is a prototype implementation of that as well in class RunOpenCode\Sax\Handler\AbstractStackedSaxHandler.
It extends RunOpenCode\Sax\Handler\AbstractSaxHandler and provides you with possibility
to get current working element via getCurrentElementName() as well as with stack size
via getStackSize().
Important notes
- Due to underlying implementation of PHP XML parser, all tag names in relevant event calls are provided uppercase. Per example,
if you have tag
<tag></tag>, in relevant event methods your check for tag name should beif ($name === 'TAG'). - Event
onParseErroris due to unrecoverable parsing error, however, it is up to you and your use case weather you are going to trigger error continue with execution. - Event
onElementDatawill trigger even if you have blank spaces only between tags in XML document.
SaxParser and StreamAdapterInterface
RunOpenCode\Sax\SaxParser is provided as utility class which ought to ease up your usage of your SaxHandler implementation. SaxHandler
uses Psr\Http\Message\StreamInterface implementation as source of XML document for parsing, however, StreamAdapters
can help you to work with various XML document sources, such as:
- Resources (file resources or PHP native streams)
- DOMDocument
- SimpleXMLElement
If you need any other type of XML document source, you can provide it by implementing RunOpenCode\Sax\Contract\StreamAdapterInterface,
and you can register it to RunOpenCode\Sax\SaxParser instance via SaxParser::addStreamAdapter() method call.
When you invoke SaxParser::parse(), before parsing, source of provided XML document will be checked against available
adapters and converted to Psr\Http\Message\StreamInterface implementation.
This library recommends guzzlehttp/psr7 and uses it as default StreamInterface implementation,
but you can use any other implementation that suits your need.
Changelog
February 21th, 2017.
- BC break: Changed api, no more callback, invocation of
parse()method should return parsing result.
February 10th, 2017.
- Dropped support for PHP 5.x
- Added PHPUnit 6.x as requirement
- Added lib exceptions
runopencode/sax 适用场景与选型建议
runopencode/sax 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.44k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「sax」 「xml parser」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 runopencode/sax 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 runopencode/sax 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 runopencode/sax 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
PHP for large XML parsing
Sax parser
Load DOM document safety
An MT940 bank statement parser for PHP
A collection of libraries for working with HTML5 markup
统计信息
- 总下载量: 11.44k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-12