How To Convert XSD into Array Using PHP

Hi friend, In this tutorial i will describe how to convert xsd into associative array.I had goggling for this but unfortunately i did not found useful answer.
Here, I am sharing my thoughts, How i was get content of xsd file, since there is no method in PHP to direct parse XSD file into Array like XML file.So I have use a trick to convert xsd into xml file and then into array.

Also checkout other related tutorials,

There are following steps to use convert XSD into array.

Step 1: Load xsd file using into Document domain.

Select Code
1
2
$doc->preserveWhiteSpace = true;
$doc->load('test.xsd');


Step 2: Save xsd file as an xml file.

Select Code
1
$doc->save('t.xml');

Step 3: Generate xml as a string and removed xsd prefix.

Select Code
1
2
$xmlfile = file_get_contents('t.xml');
$parseObj = str_replace($doc->lastChild->prefix.':',"",$xmlfile);

finally we have converted xsd into xml file, now we will xml lib function of PHP to get array.

Step 4: Load xml sting and and convert into array.

Select Code
1
2
3
4
5
$ob= simplexml_load_string($parseObj);
            $json  = json_encode($ob);
            $data = json_decode($json, true);
            echo "<pre>";
            print_r($data);

Full Source Code to Convert XSD to Array Using PHP

Select Code
1
2
3
4
5
6
7
8
9
10
11
12
<?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 "<pre>";
print_r($data);
?>

Download Full Source Code