承接 graze/xml-utils 相关项目开发

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

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

graze/xml-utils

Composer 安装命令:

composer require graze/xml-utils

包简介

XML utilities, mainly conversion between XML <=> array.

README 文档

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

XML utilities, mainly conversion between XML <=> array.

Install

Via Composer

$ composer require graze/xml-utils

Usage

Converting an array to XML

$array = [
    'child1' => 111,
    'child2' => 222
];

$xmlElement = new SimpleXMLElement('<root/>');

$xmlConverter = new Graze\XmlUtils\XmlConverter();
$xmlConverter->addArrayAsChildren($array, $xmlElement);

echo $xmlElement->asXml();

result:

?xml version="1.0"?>
<root><child1>111</child1><child2>222</child2></root>

Attributes

Attributes are supported by using @attributes key with an array of attribute name => value.

$array = [
    'child1' => [
        '@attributes' => [
            'id' => 123
        ]
    ]
];

result:

<child1 id="123"/>

Attributes with value

If a value needs to be set for an element with an attribute then the @value key should be used.

$array = [
    'child1' => [
        '@attributes' => [
            'id' => 123
        ],
        '@value' => 'some description'
    ]
];

result:

<child1 id="123">some description</child1>

Repeated elements

Repeated elements are supported with indexed arrays.

$array = [
    'child' => [
        'first',
        'second'
    ]
];

result:

<child>first</child><child>second</child>

Converting XML to an array

$xml = '<root><child1>123</child1><child2 id="1"><subchild1>456</subchild1></child2></root>';

$xmlElement = new SimpleXMLElement($xml);

$xmlConverter = new XmlConverter();
$array = $xmlConverter->convertToArray($xmlElement);

result:

[
    'child1' => '123',
    'child2' => [
        '@attributes' => [
            'id' => '1'
        ],
        'subchild1' => '456'
    ]
];

Formatting XML

Formats XML so it is easier to read.

$unformattedXml = '<?xml version="1.0" encoding="UTF-8"?><root><child1>123</child1><child2 attribute="1"><subchild1>456</subchild1></child2></root>';

$xmlFormatter = new XmlFormatter();
$formattedXml = $xmlFormatter->format($unformattedXml);

result:

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <child1>123</child1>
  <child2 attribute="1">
    <subchild1>456</subchild1>
  </child2>
</root>

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

make build test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email security@graze.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固