How to convert .xsd file into array in PHP

PHP has no direct function to convert .xsd (XML Schema Definition) file into associative array like XML file. So you have to first convert .xsd file into xml then finally parse xml file into associative array.

Also, read:

Below are few easy steps to convert .xsd file into associative array.

Step 1: First load .xsd file.

<?php
$doc->preserveWhiteSpace = true;
$doc->load('yourfile.xsd');
?>

Step 2: Save .xsd file as an xml file.

<?php
$doc->save('myxml.xml');
?>

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

<?php
$myxmlfile = file_get_contents('myxml.xml');
$parseObj = str_replace($doc->lastChild->prefix.':',"",$myxmlfile);
?>

finally .xsd file converted into xml file, Now we will use simplexml_load_string PHP function to get array.

<?php
$xml_string= simplexml_load_string($parseObj);
$json = json_encode($ob);
$data = json_decode($json, true);
print_r($data);
?>

You may also like:


Leave a Reply

Your email address will not be published. Required fields are marked *