1. Go to this page and download the library: Download vondrasoft/xml-serializer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
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);
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;
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();
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();
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('....');
xml
<test>
<element param1="value1" param2="value2">
<![CDATA[<element1 param="10">serializer</element1>]]>
</element>
</test>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.