andrewfenn/xmlreader
Composer 安装命令:
composer require andrewfenn/xmlreader
包简介
andrewfenn/xmlreader is an XML library that makes sabre/xml package easier to use
关键字:
README 文档
README
The sabre/xml library is a specialized XML reader and writer for PHP which can be downloaded here. This project is not associated with the sabre/xml project.
Sabre XML is a great PHP library for XML reading, but it can be difficult to use and make your code unessesarily complicated when you have very simple XML to parse. Therefore I have made this small extension on top of sabre/xml that allows you to access the information more natually making it easier to get what you want done.
Install
composer require andrewfenn/xmlreader -v ~1.0.0
Methods
XMLReaderElement provides the following methods...
-
array find( string )Searches through all the children or attributes, and returns an array. If you are searching for a tag the array will be a list of XMLReaderElement elements. If you are searching for an attribute your array will be of the value it contains. To search for an attribute prepend an @ to the beginning of your search term.
-
mixed findFirst( string )Searches through all the children or attributes and returns the first result found. If you are searching for a tag the result will be an XMLReaderElement. If you are searching for an attribute you will get that attributes value.
-
array children( void )Returns an array of this XMLReaderElement's children.
-
bool hasChildren( void )If the element has any XMLReaderElement children
Examples
$input = <<<XML <?xml version="1.0" encoding="UTF-8"?> <HotelMessage Version="1.0" TimeStamp="2016-05-26T11:11:50"> <Message>Hello</Message> <Hotel HotelCode="3"> <AvailableStatus> <StatusControl Start="2017-01-01" End="2017-01-02" InvTypeCode="999" RatePlanCode="888" /> <LengthsOfStay ArrivalDateBased="true"> <LengthOfStay MinMaxMessageType="MinLOS" TimeUnit="Day" Time="3" /> </LengthsOfStay> </AvailableStatus> </Hotel> <Hotel HotelCode="7"> <AvailableStatus> <StatusControl Start="2017-01-04" End="2017-01-05" InvTypeCode="111" RatePlanCode="444" /> <LengthsOfStay ArrivalDateBased="true"> <LengthOfStay MinMaxMessageType="MinLOS" TimeUnit="Day" Time="7" /> </LengthsOfStay> </AvailableStatus> </Hotel> </HotelMessage> XML; // Use Sabre as usual $reader = new \Sabre\Xml\Reader(); $reader->xml($input); // Parse in the reader object $data = (new \Sabre\Xml\XMLReaderElement())->parse($reader->parse());
Debugging
// Start accessing the data you want var_dump($data); /* Returns... object(Sabre\Xml\XMLReaderElement)#68 (4) { ["name"]=> string(12) "HotelMessage" ["namespace"]=> string(0) "" ["attributes"]=> object(stdClass)#69 (2) { ["Version"]=> string(3) "1.0" ["TimeStamp"]=> string(19) "2016-05-26T11:11:50" } ["children"]=> string(19) "Message,Hotel,Hotel" } */
Accessing a single child tag
echo "Message: ".$data->Message->value."\n"; echo "Message Type: ".$data->Message->attributes->Type."\n"; /* Returns... Message: Hello Message Type: Info */
When accessing a single child tag like this it will return the first element it finds, for multiple children see below.
echo "Hotel ID: ".$data->Hotel->attributes->HotelCode."\n"; /* Returns... Hotel ID: 3 */
Accessing multiple children tags
foreach($data->find('Hotel') as $hotel) { // Find a specific element's attribute echo "Hotel ID: ".$hotel->attributes->HotelCode."\n"; } foreach($data->children() as $tag) { if ($tag->name == 'Hotel') { echo "Hotel ID: ".$tag->attributes->HotelCode."\n"; } } /* Returns... Hotel ID: 3 Hotel ID: 7 Hotel ID: 3 Hotel ID: 7 */
Finding an array of tags or attributes
The find() function will always return an array. Your search term must match exactly to that of the Tag or Attribute.
// You can search for any tag attribute by prepending an @ infront of the attribute's name like so... foreach($data->find('@HotelCode') as $hotel_code) { echo "Hotel Code: ".$hotel_code."\n"; } // You can search for any tag like so... foreach($data->find('LengthsOfStay') as $los) { echo "Length of Stay: ".$los->findFirst('@Time')." Days\n"; } /* Returns... Hotel Code: 3 Hotel Code: 4 Length of Stay: 3 Days Length of Stay: 7 Days */
Picking up the first element or attribute
echo $data->findFirst('@TimeStamp')."\n"; echo $data->findFirst('@HotelCode')."\n"; echo $data->findFirst('Hotel')->findFirst('@RatePlanCode')."\n"; /* Returns... 2016-05-26T11:11:50 3 888 */
andrewfenn/xmlreader 适用场景与选型建议
andrewfenn/xmlreader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.34k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 01 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「dom」 「XMLReader」 「XMLWriter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andrewfenn/xmlreader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andrewfenn/xmlreader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andrewfenn/xmlreader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that provides basic utilities for creating HTML documents.
A bridge between SimpleXML and the DOM extension, plus a bunch of convenience methods
BigPipe: Pipelining web pages for high performance built in PHP.
Extends XMLReader PHP class, for simple SAX-reading and XPath queries of huge XML files.
Load DOM document safety
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
统计信息
- 总下载量: 23.34k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-01-30