lucid/xml
Composer 安装命令:
composer require lucid/xml
包简介
Xml writing and parsing utilities
README 文档
README
Installing
$ composer require lucid/xml
Testing
Run tests with:
$ ./vendor/bin/phpunit
The Parser
The Parser class can parse xml string, files, DOMDocuments, and DOMElements
into a php array.
Parsing xml strings
<?php use Lucid\Xml\Parser; $parser = new Parser; $parser->parse('<data><foo>bar</foo></data>');
Parsing xml files
<?php use Lucid\Xml\Parser; $parser = new Parser; $parser->parse('/path/to/data.xml');
Parsing a DOMDocument
<?php use Lucid\Xml\Parser; $parser = new Parser; $parser->parseDom($dom);
Parsing a DOMElement
<?php use Lucid\Xml\Parser; $parser = new Parser; $parser->parseDomElement($element);
Parser Options
Dealing with Attributes
Xml attributes are captured as an deticated section within the actual node data.
The section key defaults to @attributes, but can be changed using the setAttributesKey method.
<?php use Lucid\Xml\Parser; $xml = '<data><node id="1">some text</node></data>' $parser = new Parser; $parser->setAttributesKey('__attrs__'); $parser->parse($xml);
['data' => ['node' => ['__attrs__' => ['id' => 1], 'value' => 'some text']]];
Merging attributes
Setting Parser::mergeAttributes(true) will merge any attributes as key/value
into the data set.
$parser->setMergeAttributes(true); $parser->parse($xml);
The above example will output something simmilar to this:
['data' => ['node' => ['id' => 1, 'value' => 'some text']]];
Normalizing keys
You may specifay how keys are being transformed by setting a key normalizer callback.
The default normalizer transforms dashes to underscores and camelcase to snakecase notation.
<?php use Lucid\Xml\Parser; $parser = new Parser; $parser->setKeyNormalizer(function ($key) { // do string transfomations return $key; }); $parser->parseDomElement($element);
Set index key
This forces the parser to treat nodes with a nodeName of the given key to be handled as list.
<?php use Lucid\Xml\Parser; $parser = new Parser; $parser->setIndexKey('item');
Set a pluralizer
By default the parser will parse xml structures like
<entries> <entry>1</entry> <entry>2</entry> </entries>
To something like:
<?php ['entries' => ['entry' => [1, 2]]]
Setting a pluralizer can fix this.
Note, that a pluralizer can be any callable that takes a string and returns a string.
<?php $parser->setPluralizer(function ($string) { if ('entry' === $string) { return 'entries'; } });
<?php ['entries' => [1, 2]]
The Writer
Dumping php data to a xml string
<?php use Lucid\Xml\Writer; $writer = new Writer; $data = [ 'foo' => 'bar' ]; $writer->dump($data); // <root><foo>bar</foo></root> // set the xml root node name: $writer->dump($data, 'data'); // <data><foo>bar</foo></data>
Dumping php data to a DOMDocument
Note: this will create an instance of Lucid\Xml\Dom\DOMDocument.
<?php use Lucid\Xml\Writer; $writer = new Writer; $data = [ 'foo' => 'bar' ]; $dom = $writer->writeToDom($data);
Writer options
Set the normalizer instance
Normaly, the NormalizerInterface implementation is set for you when instantiating a new Writer, however you can set your own normalizer instance.
Note: the normalizer must implement the Lucid\Xml\Normalizer\NormalizerInterface interface.
<?php use Lucid\Xml\Writer; use Lucid\Xml\Normalizer\Normalizer; $writer = new Writer(new Normalizer); // or $writer->setNormalizer($myNormalizer);
Set the inflector
The inflector is the exact oppoite of the Parser's pluralizer. It singularizes strings.
<?php $writer->setInflector(function ($string) { if ('items' === $string) { return 'item'; } });
Set the document encoding
Default encoding is UTF-8.
<?php $writer->setEncoding($encoding); // string
Set an attribute key map
This is usefull if you want to output certain keys as xml attribute
<?php $writer->setKeyMap([ 'nodeName' => ['id', 'entry'] // nested keys 'id' and 'entry' of the key element 'nodeName' will be set as attributes instead of childnodes. ]);
Note: you can also use use addMappedAttribute($nodeName, $attributeName) to add more mapped attributes.
Set value keys
<?php $data = [ 'foo' => [ '@attributes' => [ 'bar' => 'baz' ], 'value' => 'tab' ] ];
The data structure above would dump the following xml string
<foo bar="baz"><value>tab</value></foo>
However, if you need the value node as actual value of the parent node, you may
use Writer::useKeyAsValue(string $key) to do so
<?php $writer->useKeyAsValue('value'); $writer->dump($data);
now dumps:
<foo bar="baz">tab</foo>
Writing indexed array structure
Indexed arrays will create xml structures like the example below:
$data = ['data' => [1, 2, 3]]; $writer->dump($data);
<data> <item>1</item> <item>2</item> <item>3</item> </data>
You can change the node names associated with indexed items by using the
useKeyAsIndex(string $key) method.
$writer->useKeyAsIndex('thing'); $writer->dump($data);
<data> <thing>1</thing> <thing>2</thing> <thing>3</thing> </data>
Writing arrays with none valid index keys
Arrays containing invalid indices (e.g. unordererd lists) will be treated slightly different.
$data = ['data' => [1 => 'foo', 4 => 'bar', 3 => 'baz']]; $writer->dump($data);
<data> <item index="1">foo</item> <item index="4">bar</item> <item index="3">baz</item> </data>
lucid/xml 适用场景与选型建议
lucid/xml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 12 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「parser」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lucid/xml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lucid/xml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lucid/xml 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Load DOM document safety
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 69
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-15