In this tutorial, I will describe how to convert xsd into an associative array. I had googled for this but unfortunately – I did not find a useful answer.
Here, I am sharing my thoughts, How to find an array from an XSD file, since there is no method in PHP to directly parse XSD file into an Array-like XML file. So I have used a trick to convert XSD into xml file and then into an array.
Also checkout other related tutorials,
Video Tutorial:
If you are more comfortable in watching a video that explains about xsd to array in php, then you should watch this video tutorial.
Convert XSD into an array Using PHP
We’ll follow the following steps to converts XSD into the array.
Step 1: Load xsd file using php load()
into document domain.
$doc->preserveWhiteSpace = true; $doc->load('test.xsd');
Step 2: Save xsd file as an xml file.
$doc->save('t.xml');
Step 3: Generate xml as a string and remove xsd prefix.
$xmlfile = file_get_contents('t.xml'); $parseObj = str_replace($doc->lastChild->prefix.':',"",$xmlfile);
Finally, We have converted XSD into an XML file, Let’s use the XML lib function of PHP to get an array.
Step 4: Load xml string and convert into array.
$ob= simplexml_load_string($parseObj); $json = json_encode($ob); $data = json_decode($json, true); echo ""; print_r($data);
Full Source Code to Convert XSD to Array Using PHP
<?php $doc--->$preserveWhiteSpace = true; $doc->load('test.xsd'); $doc->save('t.xml'); $xmlfile = file_get_contents('t.xml'); $parseObj = str_replace($doc->lastChild->prefix.':',"",$xmlfile); $ob= simplexml_load_string($parseObj); $json = json_encode($ob); $data = json_decode($json, true); echo ""; print_r($data); ?>