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.

Also Read :  Uninstall Remove Xampp from Linux (Ubuntu)

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

datafile.xml



  
    <title>Free Download Web Programming Courses step By step</title>
    https://pakainfo.com/
  
  
    <title>tutorials point in angularjs with PHP</title>
    https://pakainfo.com/
  


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://pakainfo.com/"},{"title":"tutorials point in angularjs with PHP","url":"https://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 "<br />Data is : <br />";
echo "******************************** <br />";
var_dump($json_data);
echo "******************************** <br />";
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 ) ) ) ) ) 

Leave a Reply

avatar
  Subscribe  
Notify of