pdeans/xml-builder
Composer 安装命令:
composer require pdeans/xml-builder
包简介
Simple and lightweight library to make generating XML a breeze.
README 文档
README
Simple and lightweight library to make generating XML a breeze.
Installation
Install via Composer.
$ composer require pdeans/xml-builder
Usage
The XML builder library extends PHP's XMLWriter extension. All XMLWriter object oriented API properties and methods are available for each XML builder instance.
First, instantiate a new XML builder class object:
use pdeans\Builders\XmlBuilder; $bulder = new XmlBuilder;
The create method is used to generate an xml tag. The create method takes the name of the root element as the first argument, and an associative array consisting of the data to build the root attribute elements and/or child elements as the second argument.
Here is a simple example:
$xml = $builder->create('Category_Add', [ '@tags' => [ 'Code' => 'Tools', 'Name' => $builder->cdata('Class Tools and Skill Kits'), ], ]);
This will produce the following xml:
<Category_Add> <Code>Tools</Code> <Name><![CDATA[Class Tools and Skill Kits]]></Name> </Category_Add>
Parent/Child Elements
Notice how the array key-values function under the @tags array from the above example. The keys represent the xml element names, and the values represent the xml element values. Child tags can also be nested following this pattern with the parent element represented by the array key, and the array value consisting of an array of the child elements as key-value pairs. This pattern can be repeated as needed to nest subsequent child elements.
Element Value Helpers
The cdata helper method can be used to wrap an element value in a <![CDATA[]]> tag, while the decimal helper method can be used to format a decimal number into a standard decimal format, rounding to 2 decimals by default and stripping out commas. The decimal helper method accepts an optional second parameter to set the precision.
// Output: <![CDATA[Class Tools and Skill Kits]]> echo $builder->cdata('Class Tools and Skill Kits'); // Output: 49.00 echo $builder->decimal(49.0000000); // Output: 49.001 echo $builder->decimal(49.0005, 3);
Reserved Keys
The @tags key represents one of 3 reserved keys (each containing shortcut key counterparts) that the xml builder uses to parse and generate the xml. The reserved keys are as follows:
@attributes Key
Shortcut: @a
The @attributes key is used to create xml element attributes. The @a key is also supported as a shortcut for the @attributes key.
Examples:
$xml = $builder->create('CategoryProduct_Assign', [ '@attributes' => [ 'category_code' => 'Food', 'product_code' => 'ale-gallon', ], ]); $xml = $builder->create('CategoryProduct_Assign', [ '@a' => [ 'category_code' => 'Food', 'product_code' => 'ale-gallon', ], ]);
XML Produced:
<CategoryProduct_Assign category_code="Food" product_code="ale-gallon"/>
@tags Key
Shortcut: @t
The @tags key accepts an associative array of data to build the root element's children. The @t key is also supported as a shortcut for the @tags key.
Examples:
$xml = $builder->create('ProductAttribute_Add', [ '@a' => [ 'product_code' => 'chest', ], '@tags' => [ 'Code' => 'lock', 'Type' => 'select', 'Prompt' => $builder->cdata('Lock'), ], ]); $xml = $builder->create('ProductAttribute_Add', [ '@a' => [ 'product_code' => 'chest', ], '@t' => [ 'Code' => 'lock', 'Type' => 'select', 'Prompt' => $builder->cdata('Lock'), ], ]);
XML Produced:
<ProductAttribute_Add product_code="chest"> <Code>lock</Code> <Type>select</Type> <Prompt><![CDATA[Lock]]></Prompt> </ProductAttribute_Add>
@value Key
Shortcut: @v
The @value key explicitly sets an xml element value. Generally, this is only required on xml elements that require both attributes and a value to be set. The @v key is also supported as a shortcut for the @value key.
Examples:
$xml = $builder->create('Module', [ '@attributes' => [ 'code' => 'customfields', 'feature' => 'fields_prod', ], '@tags' => [ 'ProductField_Value' => [ '@attributes' => [ 'product' => 'chest', 'field' => 'armor_type', ], '@value' => 'wood', ], ], ]); $xml = $builder->create('Module', [ '@a' => [ 'code' => 'customfields', 'feature' => 'fields_prod', ], '@t' => [ 'ProductField_Value' => [ '@a' => [ 'product' => 'chest', 'field' => 'armor_type', ], '@v' => 'wood', ], ], ]);
XML Produced:
<Module code="customfields" feature="fields_prod"> <ProductField_Value product="chest" field="armor_type">wood</ProductField_Value> </Module>
Note that the @tags key is used on the first level only of the associative array of tag data, as it represents the child tag data, while the other two reserved keys can be used on any sub-level throughout the associative array.
Repeated Tags
Sometimes repeated tags are used in xml, which does not play nice with associative array key-value pairs. To circumvent this, the element name is still passed as the array key, however, the array value consists of a sequential array of arrays with the tag data.
$xml = $builder->create('Order_Add', [ '@t' => [ 'Charges' => [ 'Charge' => [ [ 'Type' => 'SHIPPING', 'Description' => 'Shipping: UPS Ground', 'Amount' => 5.95 ], [ 'Type' => 'TAX', 'Description' => 'Sales Tax', 'Amount' => 2.15 ], ], ], ], ]);
XML Produced:
<Order_Add> <Charges> <Charge> <Type>SHIPPING</Type> <Description>Shipping: UPS Ground</Description> <Amount>5.95</Amount> </Charge> <Charge> <Type>TAX</Type> <Description>Sales Tax</Description> <Amount>2.15</Amount> </Charge> </Charges> </Order_Add>
Self-closing Tags
To generate a self-closing element without attributes, pass a value of null as the array value.
$xml = $builder->create('Order_Add', [ '@t' => [ 'TriggerFulfillmentModules' => null, ], ]);
XML Produced:
<Order_Add> <TriggerFulfillmentModules /> </Order_Add>
pdeans/xml-builder 适用场景与选型建议
pdeans/xml-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 94.55k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2018 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「builder」 「writer」 「xml generator」 「xml builder」 「xml writer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pdeans/xml-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pdeans/xml-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pdeans/xml-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple RSS generator library for PHP 5.5 or later. clone from Bhaktaraz Bhatta ttps://github.com/bhaktaraz/php-rss-generator
A Sphinx Query Builder extension for the Laravel Database package
Anax Database Active Record module for model classes.
Load DOM document safety
A simple package to create tables in Discord messages.
laravel facade to read/write csv file
统计信息
- 总下载量: 94.55k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 19
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-13