Skip to content

Instantly share code, notes, and snippets.

@stemar
Created October 19, 2017 22:44
Show Gist options
  • Select an option

  • Save stemar/e5ab821a3d2a7b2e08c8d487fd8dc5d5 to your computer and use it in GitHub Desktop.

Select an option

Save stemar/e5ab821a3d2a7b2e08c8d487fd8dc5d5 to your computer and use it in GitHub Desktop.
Convert XSD file to JSON file
<?php
function xsd_to_json($xsd_file, $formatted = 448) {
$xml_file = preg_replace('/\.xsd$/', '.xml', $xsd_file);
$json_file = preg_replace('/\.xsd$/', '.json', $xsd_file);
$doc = new DOMDocument();
$doc->preserveWhiteSpace = true;
$doc->load($xsd_file);
$doc->save($xml_file);
$xml = file_get_contents($xml_file);
$obj = str_replace($doc->lastChild->prefix.':', "", $xml);
$json = json_encode(simplexml_load_string($obj), $formatted);
return file_put_contents($json_file, $json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment