PHPTutorials

PHP Convert XML to JSON

Divi WordPress Theme

PHP convert xml to json is a very easy for you. please follow this tutorial and get xml to json format.

Add Xml File:
<?xml version="1.0" encoding="UTF-8"?>
<men_fashion>
  <product>
    <name>Piped Luxe Pique Polo</name>
    <price>$10.99</price>
    <description>Price Reflects 40% Off</description>
  </product>
  <product>
    <name>Striped Luxe Pique Polo</name>
    <price>$12.99</price>
    <description>Our softest polo yet is perfect for hanging out at home or heading out.</description>
  </product>
  <product>
    <name>Faded Light Wash Hyper Stretch Jean Shorts</name>
    <price>$15.99</price>
    <description>Get ready for this pair to be your new go-to this season.</description>
  </product>
  <product>
    <name>White Distressed Hyper Stretch Jean Shorts</name>
    <price>$10.50</price>
    <description>Fresh jean shorts that feature a crisp white wash with distressed details.</description>
  </product>
  <product>
    <name>Medium Wash Hyper Stretch Jean Shorts</name>
    <price>$12.99</price>
    <description>These slim-fit shorts feature our highest level of stretch for all-day comfort.</description>
  </product>
</men_fashion>
PHP Code xml to json:

how convert xml to array using bellow code:

    <?php

    $xmlfile = simplexml_load_file('sample.xml');
    $json = json_encode($xmlfile, JSON_PRETTY_PRINT);

    echo '<pre>';
    echo $json;
    echo '</pre>';

    ?>
Output:
{
    "product": [
        {
            "name": "Piped Luxe Pique Polo",
            "price": "$10.99",
            "description": "Price Reflects 40% Off"
        },
        {
            "name": "Striped Luxe Pique Polo",
            "price": "$12.99",
            "description": "Our softest polo yet is perfect for hanging out at home or heading out."
        },
        {
            "name": "Faded Light Wash Hyper Stretch Jean Shorts",
            "price": "$15.99",
            "description": "Get ready for this pair to be your new go-to this season."
        },
        {
            "name": "White Distressed Hyper Stretch Jean Shorts",
            "price": "$10.50",
            "description": "Fresh jean shorts that feature a crisp white wash with distressed details."
        },
        {
            "name": "Medium Wash Hyper Stretch Jean Shorts",
            "price": "$12.99",
            "description": "These slim-fit shorts feature our highest level of stretch for all-day comfort."
        }
    ]
}

You can check and confirm this. if have any problem please contact me.

thank you for follow this tutorial…

Khandoker Hirok

I'm an expert web developer over 6 years experience. I can fix bugs, customize and makes website and Software as per your requirements with Wordpress, PHP, Laravel and Codeigniter. My Fiverr: https://fiverr.com/hirokbd

Related Articles

Leave a Reply

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

Back to top button