Php

How To Convert XSD into Array Using PHP

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.

Related Post
$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);
?>

Download Full Source Code

Tags: xsd to xml

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories