定制 originphp/xml 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

originphp/xml

Composer 安装命令:

composer require originphp/xml

包简介

OriginPHP XML

README 文档

README

license build coverage

XML class to make making reading and writing XML easy.

Installation

To install this package

$ composer require originphp/xml

Create XML from an array

You always must pass an array with 1 root element.

To set attributes, prefix the key with @. You can also set the text value of an element using @.

use Origin\Xml\Xml;
$data = [
    'post' => [
        '@category' => 'how tos', // to set attribute use @
        'id' => 12345,
        'title' => 'How to create an XML block',
        'body' =>  Xml::cdata('A quick brown fox jumps of a lazy dog.'),
        'author' => [
            'name' => 'James'
            ]
        ]
];
    
$xml = Xml::fromArray($data);

This will return the following:

<post category="how tos">
    <id>12345</id>
    <title>How to create an XML block</title>
    <body><![CDATA["A quick brown fox jumps of a lazy dog."]]></body>
    <author>
        <name>James</name>
    </author>
</post>

For data which needed to be wrapped in CDATA, pass the data through Xml::cdata($string).

You can also pass options when creating XML from an array.

$xml = Xml::fromArray($data,[
        'version' => '1.0',
        'encoding' => 'UTF-8',
        'pretty' => true
        ]);

Sometimes you might need to repeat the tags in XML, so you can do so like this.

$data = [
'charges' => [
    'charge' => [
        [
            'amount' => 10,
            'description' => 'Shipping',
        ],
        [
            'amount' => 35,
            'description' => 'Tax',
        ],
        ]
    ]
];

Which will output this:

<?xml version="1.0" encoding="UTF-8"?>
<charges>
    <charge>
        <amount>10</amount>
        <description>Shipping</description>
    </charge>
    <charge>
        <amount>35</amount>
        <description>Tax</description>
    </charge>
</charges>

Here is an example of setting attributes (prefix the key with @) and text values (set the key to @).

$data = [
    'task' => [
        '@id' => 128,
        'name' => 'Buy milk',
        '@' => 'some text'
    ]
];

Which gives this:

<?xml version="1.0" encoding="UTF-8"?>
<task id="128">some text<name>Buy milk</name></task>

Create an Array from XML

You can also create an array from the XML using the toArray method.

$xml = '<?xml version="1.0" encoding="utf-8"?><note><to>You</to><from>Me</from><heading>Reminder</heading>  <description>Buy milk</description></note>';
$array = Xml::toArray($xml);

Namespaces

The xml utility also works with namespaces.

To set a generic namespace set the key xmlns:.

$data = [
'book' => [
    'xmlns:' => 'http://www.w3.org/1999/xhtml',
    'title' => 'Its a Wonderful Day'
    ]
];
$xml = Xml::fromArray($data);

This will output this:

<?xml version="1.0" encoding="UTF-8"?>
<book xmlns="http://www.w3.org/1999/xhtml">
<title>Its a Wonderful Day</title>
</book>

You can setup custom namespaces like this:

$data = [
    'student:record' => [
        'xmlns:student' => 'https://www.originphp.com/student',
        'student:name' => 'James',
        'student:phone' => '07986 123 4567'
    ]
];

Which will give you this

<?xml version="1.0" encoding="UTF-8"?>
<student:record xmlns:student="https://www.originphp.com/student">
    <student:name>James</student:name>
    <student:phone>07986 123 4567</student:phone>
</student:record>

Lets take an example from w3.org and re-create this using an array.

So this is what we want to produce:

<book xmlns='urn:loc.gov:books'
      xmlns:isbn='urn:ISBN:0-395-36341-6'>
    <title>Cheaper by the Dozen</title>
    <isbn:number>1568491379</isbn:number>
</book>

To do this in an array (you could use the toArray method if you have the existing XML) set it up as follows.

$data = [
    'book' => [
        'xmlns:' => 'urn:loc.gov:books',
        'xmlns:isbn' => 'urn:ISBN:0-395-36341-6',
        'title' => 'Cheaper by the Dozen',
        'isbn:number' => '1568491379' 
    ]
];

originphp/xml 适用场景与选型建议

originphp/xml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.34k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「xml」 「parser」 「originPHP」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 originphp/xml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 originphp/xml 我们能提供哪些服务?
定制开发 / 二次开发

基于 originphp/xml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-12