extphp / xml-to-json
一个XML到JSON的转换器,可以正确地保留属性。
v0.2.0
2019-08-21 08:29 UTC
Requires (Dev)
- phpunit/phpunit: >=5.0
This package is auto-updated.
Last update: 2024-09-21 20:31:02 UTC
README
一个XML到JSON的转换器,可以正确地保留属性。
安装
composer require extphp/xml-to-json
用法
通用用法,当SimpleXMLElement实例已存在时非常有用。
use ExtPHP\XmlToJson\XmlToJsonConverter; $string = '<node attr1="value1" attr2="value2"><child>child value</child></node>'; $xml = simplexml_load_string($string); $converter = new XmlToJsonConverter($xml); $converter->toArray(); // convert xml to array $converter->toJson(); // convert xml to json
当需要将XML字符串转换为数组或JSON时,这是一个快速的方法。
use ExtPHP\XmlToJson\JsonableXML; $xml = new JsonableXML('<node attr1="value1" attr2="value2"><child>child value</child></node>'); json_encode($xml); // convert xml to json // These methods are also available directly on the xml object. $xml->toArray(); // convert xml to array $xml->toJson(); // convert xml to json