traineratwot/xmltodict

类似于 python xmltodict 的 xml 解析器

1.1.1 2023-05-20 17:30 UTC

This package is auto-updated.

Last update: 2024-09-30 01:41:21 UTC


README

Python 的 XmlToDict 的完整类似物,但用 PHP 编写

Python 的 XmlToDict 的完整类似物,但用 PHP 编写

安装

composer require traineratwot/xmltodict

用法

$array = XmlToDict::load(__DIR__ . '/test.xml');

$xml = file_get_contents(__DIR__ . '/test.xml')
$array = XmlToDict::parse($xml);

示例

<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book id="1">
		<title meta="test">Harry Potter and the Philosopher's Stone</title>
		<author>J.K. Rowling</author>
		<publisher>Bloomsbury</publisher>
		<year>1997</year>
	</book>
	<book id="2">
		<title meta="test2">The Lord of the Rings</title>
		<author>J.R.R. Tolkien</author>
		<publisher>George Allen &amp; Unwin</publisher>
		<year>1954</year>
	</book>
	<item>Text</item>
	<item2 desc="Lorem">Text</item2>

	<item3></item3>
</books>
{
    "books": {
        "book": [
            {
                "@id": "1",
                "title": {
                    "@meta": "test",
                    "#text": "Harry Potter and the Philosopher's Stone"
                },
                "author": "J.K. Rowling",
                "publisher": "Bloomsbury",
                "year": "1997"
            },
            {
                "@id": "2",
                "title": {
                    "@meta": "test2",
                    "#text": "The Lord of the Rings"
                },
                "author": "J.R.R. Tolkien",
                "publisher": "George Allen & Unwin",
                "year": "1954"
            }
        ],
        "item": "Text",
        "item2": {
            "@desc": "Lorem",
            "#text": "Text"
        },
        "item3": null
    }
}

支持

php 7.3+