OriginPHP XML

2.1.0 2021-06-02 07:11 UTC

This package is auto-updated.

Last update: 2024-09-29 05:29:51 UTC


README

license build coverage

XML类,使读取和写入XML变得简单。

安装

要安装此包

$ composer require originphp/xml

从数组创建XML

您始终必须传递一个包含1个根元素的数组。

要设置属性,请在键前加@。您还可以使用@设置元素的文本值。

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);

这将返回以下内容

<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>

对于需要用CDATA包装的数据,请通过Xml::cdata($string)传递数据。

在从数组创建XML时,您也可以传递选项。

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

有时您可能需要在XML中重复标签,您可以这样做。

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

这将输出以下内容

<?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>

以下是设置属性(键前加@)和文本值(键为@)的示例。

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

这将得到以下结果

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

从XML创建数组

您还可以使用toArray方法从XML创建数组。

$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);

命名空间

xml工具还支持命名空间。

要设置一个通用命名空间,请设置键xmlns:

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

这将输出以下内容

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

您可以通过这种方式设置自定义命名空间

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

这将给您以下结果

<?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>

让我们以w3.org的示例为例,并使用数组重新创建。

这就是我们想要生成的结果

<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>

在数组中这样做(如果您有现有的XML,可以使用toArray方法)设置如下。

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