Convert XML to JSON PHP with example

Hello buddy, I hope you are doing well with this article, we will learn how to convert XML to JSON in PHP with a simple example.

To convert the XML data to JSON in PHP we will use PHP predefined function simplexml_load_string. It will Interpret an XML file into an object. You read more information about the simplexml_load_string.from the PHP official website.

We will also use PHP predefined function json_encode, to encode the object data which we have to get from the simplexml_load_string function.

We will follow below steps to convert xml to json

  1. Get your xml Data
  2. Put data in simplexml_load_string
  3. Test the scenario

Step:1 Get your xml Data

Below is an example of my XML data. The same data we will convert into JSON.

<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>

Step:2 Put your xml string into the code

Now we will use the below code to convert the XML string into the JSON.

$xmlData = '<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>';

$xmlObject = simplexml_load_string($xmlData);
$jsonData = json_encode($xmlObject, JSON_PRETTY_PRINT);

print_r($jsonData);

Step:3 Test the integration

When you run the above command it will give you the below output

{

    "food": [

        {

            "name": "Belgian Waffles",

            "price": "$5.95",

            "description": "Two of our famous Belgian Waffles with plenty of real maple syrup",

            "calories": "650"

        },

        {

            "name": "Strawberry Belgian Waffles",

            "price": "$7.95",

            "description": "Light Belgian waffles covered with strawberries and whipped cream",

            "calories": "900"

        },

        {

            "name": "Berry-Berry Belgian Waffles",

            "price": "$8.95",

            "description": "Light Belgian waffles covered with an assortment of fresh berries and whipped cream",

            "calories": "900"

        },

        {

            "name": "French Toast",

            "price": "$4.50",

            "description": "Thick slices made from our homemade sourdough bread",

            "calories": "600"

        },

        {

            "name": "Homestyle Breakfast",

            "price": "$6.95",

            "description": "Two eggs, bacon or sausage, toast, and our ever-popular hash browns",

            "calories": "950"

        }

    ]

}

I hope now you understood how we can convert XML to JSON in PHP in a very easy manner.

0 0 votes
Article Rating

Do you want to hire us for your Project Work? Then Contact US.
Spread the love
CategoriesPHP
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x