承接 andrewfenn/xmlreader 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

andrewfenn/xmlreader

最新稳定版本:1.0.0

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 */

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固