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.
and We remove the newlines in data content, and then returns and tabs.
Code of dataXMLtoJSONfile() – Convert XML to JSON in PHP
datafile.xml
<?xml version="1.0" encoding="UTF-8"?> <websites> <site> <title>Free Download Web Programming Courses step By step</title> <url>http://techieupgrader.in/</url> </site> <site> <title>tutorials point in angularjs with PHP</title> <url>http://techieupgrader.in/</url> </site> </websites>
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":"http://techieupgrader.in/"},{"title":"tutorials point in angularjs with PHP","url":"http://techieupgrader.in/"}]}
Simple Example – XML-to-JSON
$data_result = '<Datasurround> <NewdatamainCat> <firstTag data_val1="false" data_val2="true" data_val3="false" data_val4="false" /> <secondTag data_val1="false" data_val2="true" data_val3="false" data_val4="false" /> <datamyTag><singleTag interndata_value="xml to json data"/></datamyTag> </NewdatamainCat> </Datasurround>'; $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 ) ) ) ) )
0 comments: