Convert XML to JSON in PHP With Example - Codewolfy

Converting XML to JSON in PHP is a common task for developers working with data interchange formats. When you want to read XML data and convert it into a JSON format, PHP provides simple ways to accomplish this. This blog will guide you through the process of PHP convert XML to JSON with practical examples that anyone can follow. Whether you are dealing with APIs, data exports, or configuration files, learning how to convert XML to Json in PHP will make your work easier and more efficient.

Generating an XML Sitemap file named sitemap.xml is also important for SEO, helping search engines index your website better. For example, the sitemap.xml can contain URLs, image counts, and last modification dates of your pages. This structured file improves your site’s visibility and helps in managing your content updates effectively.

<?xml version="1.0" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://codewolfy.com/blogs/</loc>
    <images>0</images>
    <lastmod>2024-04-06 13:13 +00:00</lastmod>
  </url>
  <url>
    <loc>https://codewolfy.com/privacy-policy/</loc>
    <images>0</images>
    <lastmod>2024-05-26 12:12 +00:00</lastmod>
  </url>
  <url>
    <loc>https://codewolfy.com/terms-of-use/</loc>
    <images>0</images>
    <lastmod>2024-05-26 12:22 +00:00</lastmod>
  </url>
  <url>
    <loc>https://codewolfy.com/contact-us/</loc>
    <images>0</images>
    <lastmod>2024-05-26 12:25 +00:00</lastmod>
  </url>
  <url>
    <loc>https://codewolfy.com/about-us/</loc>
    <images>0</images>
    <lastmod>2024-05-26 12:29 +00:00</lastmod>
  </url>
</urlset>

We will utilize and use this sitemap file in further examples to convert it to JSON content.

Example to Convert XML Data to Json

To convert XML site to first you need to load it using PHP’s in-built function simplexml_load_file(). The function will load XML content into PHP class object and then you can utilize it for your own purpose. Let’s see how you can convert XML data to JSON using PHP:

<?php

$xmlObject = simplexml_load_file('sitemap.xml');

$jsonData = json_encode($xmlObject, JSON_PRETTY_PRINT);

print_r($jsonData);

Here it will load data into object and with help of json_encode() function it will be converted into JSON object. Here, we are just printing it to the user into formatted way. Output will look like something below:

{
    "url": [
        {
            "loc": "https:\/\/codewolfy.com\/blogs\/",
            "images": "0",
            "lastmod": "2024-04-06 13:13 +00:00"
        },
        {
            "loc": "https:\/\/codewolfy.com\/privacy-policy\/",
            "images": "0",
            "lastmod": "2024-05-26 12:12 +00:00"
        },
        {
            "loc": "https:\/\/codewolfy.com\/terms-of-use\/",
            "images": "0",
            "lastmod": "2024-05-26 12:22 +00:00"
        },
        {
            "loc": "https:\/\/codewolfy.com\/contact-us\/",
            "images": "0",
            "lastmod": "2024-05-26 12:25 +00:00"
        },
        {
            "loc": "https:\/\/codewolfy.com\/about-us\/",
            "images": "0",
            "lastmod": "2024-05-26 12:29 +00:00"
        }
    ]
}

You can also save this to file by using below code example:

<?php
$xml = simplexml_load_file('sitemap.xml');

$json = json_encode($xml, JSON_PRETTY_PRINT);

file_put_contents('sitemap.json', $json);

echo "JSON data has been saved to sitemap.json";
?>

It will create sitemap.json file with same content from previous output.

Conclusion

Knowing how to convert XML to JSON in PHP expands your ability to handle different data formats easily. PHP’s built-in functions make this conversion easy and efficient, suitable for real-world applications used for API development and content management. It helps you work flexibly with diverse data sources and optimize your web projects seamlessly.

If you are familiar with Laravel framework and perform similar operation. You must checkout this post on “Read XML File in Laravel“.