vondrasoft / xml-serializer
将数组转换为XML,反之亦然。
v1.0.1
2021-12-31 08:30 UTC
Requires
- php: >=8.0
- ext-dom: *
- ext-libxml: *
- ext-simplexml: *
Requires (Dev)
- jetbrains/phpstorm-attributes: ^1.0
- phpstan/phpstan: ^1.2.0
- phpunit/phpunit: ^9.5
- slevomat/coding-standard: ^7.0
- squizlabs/php_codesniffer: *
README
此软件包可以将XML序列化为集合(数组、JSON)以及反向序列化,适用于您的API。
它与PHP 8.0+兼容(并经过测试)。代码覆盖率达到了100%,使用了codesniffer和phpstan的最高级别。
安装
将vondrasoft/xml-serializer
添加到您的composer.json
文件中或
composer require vondrasoft/xml-serializer
用法
将数组序列化为XML
<?php use XmlSerializer\Factory\ElementCollectionFactory; use XmlSerializer\Inspector\CollectionInspector; use XmlSerializer\Serializer\XmlSerializer; use XmlSerializer\XmlSerializerManager; $input = [ [ 'name' => 'test', 'value' => [ [ 'cdata' => true, 'name' => 'element', 'attributes' => [ [ 'name' => 'param1', 'value' => 'value1', ], [ 'name' => 'param2', 'value' => 'value2', ], ], 'value' => [ [ 'name' => 'element1', 'attributes' => [ [ 'name' => 'param', 'value' => '10', ], ], 'value' => 'serializer', ], ], ], ], ], ]; $manager = new XmlSerializerManager(new XmlSerializer(), new CollectionInspector()); echo $manager->getXmlFromArray($input);
输出
<test> <element param1="value1" param2="value2"> <![CDATA[<element1 param="10">serializer</element1>]]> </element> </test>
将XML反序列化为集合
<?php use XmlSerializer\Factory\ElementCollectionFactory; use XmlSerializer\Serializer\XmlSerializer; $inputXml = ' <vehicle> <brand code="xx">Xexe</brand> <data code="dataset"> <model type="string">BestModel</model> <risk> <optional>Zero</optional> <primary>First</primary> </risk> </data> </vehicle>'; $serializer = new XmlSerializer(); $collection = $serializer->deserialize($inputXml); // collections implements JsonSerializable interface, so you can transform to json them easily echo json_encode($collection);
输出
[ { "name": "vehicle", "value": [ { "name": "brand", "attributes": [ { "name": "code", "value": "xx" } ], "value": "Xexe" }, { "name": "data", "attributes": [ { "name": "code", "value": "dataset" } ], "value": [ { "name": "model", "attributes": [ { "name": "type", "value": "string" } ], "value": "BestModel" }, { "name": "risk", "value": [ { "name": "optional", "value": "Zero" }, { "name": "primary", "value": "First" } ] } ] } ] } ]
手动创建集合
<?php use XmlSerializer\Collection\ElementCollection; use XmlSerializer\Factory\ElementCollectionFactory; use XmlSerializer\Model\Element; use XmlSerializer\Serializer\XmlSerializer; $collection = new ElementCollection(); $firstElement = (new Element('firstElement'))->setValue('firstValue'); $secondElement = (new Element('secondElement'))->setValue('secondValue'); $collection ->addElement($firstElement) ->addElement($secondElement); $xmlCollection = new ElementCollection(); $rootElement = (new Element('main'))->setElements($collection); $xmlCollection->addElement($rootElement); $serializer = new XmlSerializer(); $output = $serializer->serialize($xmlCollection); echo $output;
输出
<main> <firstElement>firstValue</firstElement> <secondElement>secondValue</secondElement> </main>
集合检查器
<?php use XmlSerializer\Factory\ElementCollectionFactory; use XmlSerializer\Inspector\CollectionInspector; use XmlSerializer\Serializer\XmlSerializer; $inputXml = ' <vehicle> <brand code="xx">Xexe</brand> <data code="dataset"> <model type="string">BestModel</model> <risk> <optional>Zero</optional> <primary>First</primary> </risk> </data> </vehicle>'; $serializer = new XmlSerializer(); $collection = $serializer->deserialize($inputXml); $inspector = new CollectionInspector($collection); // will print "BestModel" echo $inspector->getElementByPath('vehicle.data.model')->getValue();
存在同名元素的问题。因此,您可以指定一个索引来指定元素。
<?php use XmlSerializer\Factory\ElementCollectionFactory; use XmlSerializer\Inspector\CollectionInspector; use XmlSerializer\Serializer\XmlSerializer; $inputXml = ' <notepad> <param>first</param> <param>second</param> <param> <note>one</note> <note>two</note> </param> </notepad> '; $serializer = new XmlSerializer(); $collection = $serializer->deserialize($inputXml); $inspector = new CollectionInspector($collection); // will print "first" echo $inspector->getElementByPath('notepad.param[0]')->getValue(); // will print "second" echo $inspector->getElementByPath('notepad.param[1]')->getValue(); // will print "two" echo $inspector->getElementByPath('notepad.param[2].note[1]')->getValue();
XmlSerializerManager
这是软件包中的主要类,您可以通过依赖注入将其注入到服务中,并简单使用它。
<?php use XmlSerializer\Inspector\CollectionInspector; use XmlSerializer\Serializer\XmlSerializer; use XmlSerializer\XmlSerializerManager; $inputXml = ' <notepad> <param>first</param> <param>second</param> <param> <note>one</note> <note>two</note> </param> </notepad> '; $manager = new XmlSerializerManager(new XmlSerializer(), new CollectionInspector()); // you can call serializer methods by getSerializer ... $collection = $manager->getSerializer()->deserialize($inputXml); // but you can use it directly on the manager level $array = $manager->getArrayFromXml($inputXml); // will print the same array, like calling $collection->toArray(); var_dump($array); // you can get json from xml and etc.. getXmlFromArray, getXmlFromJson, getArrayFromXml $json = $manager->getJsonFromXml($inputXml); //you can use inspector from manager by call $inspector = $manager->getCollectionInspector(); // you must set the collection if you are using dependency injection without inject collection to constructor $inspector->setCollection($collection); $element = $inspector->getElementByPath('....');