承接 ikr/xml-fu 相关项目开发

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

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

ikr/xml-fu

最新稳定版本:v1.1.0

Composer 安装命令:

composer require ikr/xml-fu

包简介

Handy utility functions to query XML. Was written to simplify dealing with SOAP responses.

README 文档

README

Build Status

About

Handy utility functions to query XML. Was initially written to simplify dealing with raw SOAP responses, but can also be useful in general, enabling a more functinal style of extracting data from XML.

Installation

Requires PHP 5.3+ and Composer.

composer require ikr/xml-fu:~1.1

API

Core

XmlFu\value($xml, $xpath, $default)

Searches the $xml string for the first node matching the given $xpath, and returns its string value. If the node wasn't found, the $default value is returned.

XmlFu\firstNode($xml, $xpath)

Returns a SimpleXMLElement instance for the first node matching the given $xpath in the given $xml string.

XmlFu\chunks($xml, $xpath)

Finds all the nodes in the passed $xml string matching the given $xpath, and returns an array of their asXML() serializations -- strings.

XmlFu\attrValues($xml, $elementXpath, $attrName)

Finds all the elements in the passed $xml string matching the given $elementXpath, and returns an array of their $attrName attribute values -- one string value per element.

Core without parsing

There are *Impl versions of the core functions, accepting $rootElement-s -- SimpleXMLElement instances instead of the $xml strings.

XmlFu\valueImpl($rootElement, $xpath, $default)
XmlFu\firstNodeImpl($rootElement, $xpath)
XmlFu\chunksImpl($rootElement, $xpath)
XmlFu\attrValuesImpl($rootElement, $elementXpath, $attrName)

That way you can avoid unnecessary multiple parsing runs when querying for multiple values.

Dealing with the default namespace

SimpleXMLElement::xpath() have some quirks dealing with the default XML namespace declaration: the xmlns="..." attribute. As soon as it's present, all the nodes contained by the element with xmlns declared, and the declaring element itself must be prefixed with a namespace in XPath queries. XmlFu conveniently aliases the default namespace with underscore -- _

Thus,

$xml = <<<XML
    <OTA_PingRS xmlns="http://www.opentravel.org/OTA/2003/05">
        <Success />
        <EchoData>Hey!</EchoData>
    </OTA_PingRS>    
XML;

echo XmlFu\value($xml, '//_:EchoData', null); // 'Hey!'

Extras

XmlFu\removeXmlProcessingInstruction($xml)

Removes the <?xml ... declaration from the passed $xml string. The removal is done in a safe way: no reg-exps, honest parsing.

XmlFu\rootTagName($xml)

The name of the topmost XML element in the passed $xml string.

Development status

Used in production.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-29