定制 andrewfenn/xmlreader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

andrewfenn/xmlreader

Composer 安装命令:

composer require andrewfenn/xmlreader

包简介

andrewfenn/xmlreader is an XML library that makes sabre/xml package easier to use

README 文档

README

Software License Scrutinizer Code Quality Total Downloads Total Downloads Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 andrewfenn/xmlreader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 23.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 24
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2017-01-30