mehr-it/easy-xml
Composer 安装命令:
composer require mehr-it/easy-xml
包简介
Easy XML handling (read/write) for large files
README 文档
README
Writing XML files
The XMLBuilder class offers a similar API as the PHP's XMLWriter.
In fact it utilizes it internally. But most of the time this way
of generating XML is not very intuitive and your source code does
not reveal the XML structure clearly.
Therefore the XMLBuilder offers a powerful array based
interface which reveals the XML structure in your PHP code.
It's always a good idea if your source code reveals it's purpose
at first glance.
Getting started
The following example demonstrates how to create a basic XML with nested nodes:
$builder = new XMLBuilder();
$builder
->beginDocument()
->write([
'root' => [
'child' => 'content',
'anotherChild' => 15
]);
The tag name is represented by the array key. The content is determined by the array value:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>content</child>
<anotherChild>15</anotherChild>
</root>
Adding attributes
To add attributes to our XML nodes, we prefix the key with an open XML bracket ("<") and pass an array with attribute names as keys prefixed with "@". The content is defined by the item simply named "@". Or you use an numeric array and specify the the tag name inside using the ">" key:
$builder->write([
'<root' => [
'@author' => 'Paul'
'@age' => 40,
'@' => [
'<child' => [
'@age' => '4'
'@' => 'Lukas'
],
// alternative syntax:
[
'>' => 'wife',
'@age' => 36,
'@' => 'Lisa'
]
]
]
]);
As you see, you even can mix the different methods of defining XML nodes. The resulting XML would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<root author="Paul" age="40">
<child age="4">content</child>
<wife age="36">Lisa</wife>
</root>
Multiple nodes with same name
Sometimes you need to define multiple siblings with the same name. There are two ways to do so. Either you wrap each node in an array or you use the ">" key to define the tag name:
$builder->write([
'root' => [
['child' => 'Lukas'],
['child' => 'Sophie'],
// alternative syntax:
[
'>' => 'child',
'@' => 'Michael'
],
[
'>' => 'child',
'@' => 'Zoe'
]
]
]);
Passing closures
Often the XML structure depends on the data to write. To keep a fluent interface and a visual structure of the XML in your source, you may pass a Closure to generate dynamic parts of your XML:
$builder->write([
'root' => function(XMLBuilder $bld) use ($person) {
if ($person instanceof Child)
$bld->write(['child' => $person->getName()];
else
$bld->write(['adult' => $person->getName()];
});
Upgrade notes
2.x
- Starting with version 2.x, all given paths and attribute name are expected to be UTF-8 encoded. Automatic encoding conversion was removed to increase performance.
mehr-it/easy-xml 适用场景与选型建议
mehr-it/easy-xml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.93k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mehr-it/easy-xml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mehr-it/easy-xml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-06