cdekok/xml-transform
Composer 安装命令:
composer require cdekok/xml-transform
包简介
XML to Array transformer
关键字:
README 文档
README
PHP XML Transformer
This library is useful to map xml values to array values, with xpath queries.
Installation
composer require cdekok/xml-transform
Usage
List of data
// Optional add namespaces in the XML $namespaces = ['oai' => 'http://www.openarchives.org/OAI/2.0/']; // Define the mapping for the array that you want to have filled $mapping = [ 'id' => [ 'xpath' => './/oai:identifier/text()' ], 'material' => [ 'xpath' => './/oai:material/text()', 'repeatable' => true // If elements are repeatable set this option so an array will be returned ], ]; $data = (new \XmlTransform\Mapper($mapping, '//oai:OAI-PMH/oai:ListRecords/oai:record', $namespaces)) ->from('somefile.xml') ->transform(); // $data will contain something like [ ['id' => '12', 'material' => ['paint', 'pencil']], ['id' => '13', 'material' => ['pen', 'pencil']], ]
Single array
For convience it's also possible to only map to 1 array instead of a list of results.
$data = (new \XmlTransform\Mapper($mapping, '//oai:OAI-PMH/oai:ListRecords/oai:record', $namespaces)) ->from('somefile.xml') ->transformOne(); // $data will contain something like ['id' => '12', 'material' => ['paint', 'pencil']]
Repeatable nested elements
$mapping = [ 'id' => ['xpath' => './/oai:objectid/text()'], 'creator' => [ 'repeatable' => true, // Mark the element as repeatable 'context' => './/oai:constituent', // new context for the nested elements 'values' => [ 'name' => ['xpath' => './/text()'], 'death_date' => ['xpath' => './/@death_date'], ] ] ]; $transformer = new \XmlTransform\Mapper($mapping, '//oai:record', $namespaces); $result = $transformer->from($xml)->transformOne(); // Result will contain something like this [ 'id' => '3517', 'creator' => [ ['name' => 'Rembrandt', 'death_date' => '1669'], ['name' => 'Johannes Mock', 'death_date' => '1884'], ['name' => 'Georg Friedrich Schmidt', 'death_date' => '1775'], ] ]
Filter values
Filter empty values from the returned array
$transformer->from($xml)->filter()->transform();
Optional elements (contexts)
If there are optional elements with a context in the xml you will need to enable the optional setting to prevent an ContextNotFoundException
$mapping = [ 'record' => [ 'context' => './/data', 'values' => [ 'title' => ['xpath' => './/title/text()'], 'creator' => ['xpath' => './/creator/text()'], // optional ] ], ]; $transformer = new \XmlTransform\Mapper($mapping, '//record'); $result = $transformer->from($xml)->optionalElements()->filter()->transform(); // Result creator is missing. [ 'record' => [ 'title' => 'test', 'creator' => 'Bert', ], ], [ 'record' => [ 'title' => 'test 2', ] ]
Development
After running composer install grumphp will watch codestyles and unit tests before commits.
To manually check the code style / unit tests run composer run test
To format the code automatically run composer run format
To generate test coverage run composer run report
This project follows git flow for commits
cdekok/xml-transform 适用场景与选型建议
cdekok/xml-transform 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.68k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「mapping」 「transformer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cdekok/xml-transform 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cdekok/xml-transform 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cdekok/xml-transform 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Serializer transformers outputting valid API responses in JSON, JSON API and HAL+JSON API formats.
Symfony 2 & 3 JSON API Transformer Package
Forms mapping of Doctrine ORM entities for Nette Framework
a library to simplify handling(filter, map, ...) collections in php
A simple library that allows transform any kind of data to native php data or whatever
HAL+JSON & HAL+XML API transformer outputting valid API responses.
统计信息
- 总下载量: 5.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-11-26