originphp/xml 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

originphp/xml

最新稳定版本:2.1.0

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' 
    ]
];

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固