Posted inProgramming / Mysql / Mysqli / php

PHP Convert XML to JSON with Attributes

PHP Convert XML to JSON with Attributes

Today, We want to share with you PHP Convert XML to JSON with Attributes.
In this post we will show you Convert XML to JSON using PHP with example, hear for Convert XML to JSON in PHP – Sample Code, Scripts we will give you demo and example for implement.
In this post, we will learn about PHP convert XML to JSON with an example.

It Use server-side code-script to streamline Ajax.

First of all we need to get all data contents of the XML file format,and we need to use the PHP function file_get_contents() and all pass data it the URL to the (.xml)XML file format.
and We remove the newlines in data content, and then returns and tabs.

Code of dataXMLtoJSONfile() – Convert XML to JSON in PHP

datafile.xml




  
    Free Download Web Programming Courses step By step
    
Pakainfo – Best Online Web Development Tutorials
tutorials point in angularjs with PHP
Pakainfo – Best Online Web Development Tutorials

xml to json with php

index.php


// simple converts steps XML file content to JSON format
// php script receives the get URL address of the XML data file. and the Returns a string data with the JSON format object to get
function dataXMLtoJSONfile($dataxml) {
  $res_xml_cnt_data = file_get_contents($dataxml);    // gets XML data content from file format
  $res_xml_cnt_data = str_replace(array("\n", "\r", "\t"), '', $res_xml_cnt_data);    // all removes newlines content, and the returns and tabs data

  //data replace double quotes all content with single quotes data,go to ensure the basic XML function can parse data the XML file format
  $res_xml_cnt_data = trim(str_replace('"', "'", $res_xml_cnt_data));
  $filesimpleXml = simplexml_load_string($res_xml_cnt_data);

  return json_encode($filesimpleXml);    // data returns a string value with JSON format object
}

echo dataXMLtoJSONfile('datafile.xml');

Result : xml2json using php


{"site":[{"title":"Free Download Web Programming Courses step By step","url":"https://www.pakainfo.com/"},{"title":"tutorials point in angularjs with PHP","url":"https://www.pakainfo.com/"}]}

Simple Example – XML-to-JSON


$data_result = '
    
        
        
        
    
';
$xml_data = simplexml_load_string($data_result);
$json_data = json_decode(json_encode($xml_data));

echo json_encode($xml_data);
echo "
Data is :
"; echo "********************************
"; var_dump($json_data); echo "********************************
"; print_r($json_data);

Result : xml to json using PHP


{"NewdatamainCat":{"firstTag":{"@attributes":{"data_val1":"false","data_val2":"true","data_val3":"false","data_val4":"false"}},"secondTag":{"@attributes":{"data_val1":"false","data_val2":"true","data_val3":"false","data_val4":"false"}},"datamyTag":{"singleTag":{"@attributes":{"interndata_value":"xml to json data"}}}}}
Data is :
********************************
object(stdClass)#15 (1) 
{ ["NewdatamainCat"]=> object(stdClass)#9 (3) 
{ ["firstTag"]=> object(stdClass)#8 (1) 
{ ["@attributes"]=> object(stdClass)#7 (4) 
	{ 
		["data_val1"]=> string(5) "false" 
		["data_val2"]=> string(4) "true" 
		["data_val3"]=> string(5) "false" 
		["data_val4"]=> string(5) "false" 
	} 
} 
["secondTag"]=> object(stdClass)#11 (1) 
{ ["@attributes"]=> object(stdClass)#10 (4) 
	{ 
		["data_val1"]=> string(5) "false" 
		["data_val2"]=> string(4) "true" 
		["data_val3"]=> string(5) "false" 
		["data_val4"]=> string(5) "false" 
	} 
} 
["datamyTag"]=> object(stdClass)#14 (1) { ["singleTag"]=> object(stdClass)#13 (1) { ["@attributes"]=> object(stdClass)#12 (1) { ["interndata_value"]=> string(16) "xml to json data" } } } } } ********************************
stdClass Object ( [NewdatamainCat] => stdClass Object ( [firstTag] => stdClass Object ( [@attributes] => stdClass Object ( [data_val1] => false [data_val2] => true [data_val3] => false [data_val4] => false ) ) [secondTag] => stdClass Object ( [@attributes] => stdClass Object ( [data_val1] => false [data_val2] => true [data_val3] => false [data_val4] => false ) ) [datamyTag] => stdClass Object ( [singleTag] => stdClass Object ( [@attributes] => stdClass Object ( [interndata_value] => xml to json data ) ) ) ) ) 

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype