PHP convert XML to JSON
I am trying to convert xml to json in php. If I do a simple convert using simple xml and json_encode none of the attributes in the xml show.$xml = simplexml_load_file("states.xml");echo...
View ArticleAnswer by Michael Fenwick for PHP convert XML to JSON
Looks like the $state->name variable is holding an array. You can usevar_dump($state)inside the foreach to test that.If that's the case, you can change the line inside the foreach to $states[]=...
View ArticleAnswer by Bryan Hadlock for PHP convert XML to JSON
I figured it out. json_encode handles objects differently than strings. I cast the object to a string and it works now.foreach($xml->children() as $state){ $states[]= array('state' =>...
View ArticleAnswer by Coreus for PHP convert XML to JSON
Sorry for answering an old post, but this article outlines an approach that is relatively short, concise and easy to maintain. I tested it myself and works pretty...
View ArticleAnswer by Husky for PHP convert XML to JSON
I've used Miles Johnson's TypeConverter for this purpose. It's installable using Composer.You could write something like this using it:<?phprequire 'vendor/autoload.php';use...
View ArticleAnswer by ChrisR for PHP convert XML to JSON
If you would like to only convert a specific part of the XML to JSON, you can use XPath to retrieve this and convert that to JSON.<?php$file = @file_get_contents($xml_File, FILE_TEXT);$xml = new...
View ArticleAnswer by Antonio Max for PHP convert XML to JSON
Json & Array from XML in 3 lines:$xml = simplexml_load_string($xml_string);$json = json_encode($xml);$array = json_decode($json,TRUE);
View ArticleAnswer by Coder Of Salvation for PHP convert XML to JSON
A common pitfall is to forget that json_encode() does not respect elements with a textvalue and attribute(s). It will choose one of those, meaning dataloss.The function below solves that problem. If...
View ArticleAnswer by FTav for PHP convert XML to JSON
I guess I'm a bit late to the party but I have written a small function to accomplish this task. It also takes care of attributes, text content and even if multiple nodes with the same node-name are...
View ArticleAnswer by Ajay Kumar for PHP convert XML to JSON
Try to use this$xml = ... // Xml file data// first approach$Json = json_encode(simplexml_load_string($xml));---------------- OR -----------------------// second approach$Json =...
View ArticleAnswer by Octavio Perez Gallegos for PHP convert XML to JSON
$templateData = $_POST['data'];// initializing or creating array$template_info = $templateData;// creating object of SimpleXMLElement$xml_template_info = new SimpleXMLElement("<?xml...
View ArticleAnswer by Peter Krauss for PHP convert XML to JSON
All solutions here have problems!... When the representation need perfect XML interpretation (without problems with attributes) and to reproduce all text-tag-text-tag-text-... and order of tags. Also...
View ArticleAnswer by TheStoryCoder for PHP convert XML to JSON
This is an improvement of the most upvoted solution by Antonio Max, which also works with XML that has namespaces (by replacing the colon with an underscore). It also has some extra options (and does...
View ArticleAnswer by Marco Leuti for PHP convert XML to JSON
Optimizing Antonio Max answer:$xmlfile = 'yourfile.xml';$xmlparser = xml_parser_create();// open a file and read data$fp = fopen($xmlfile, 'r');//9999999 is the length which fread stops to...
View ArticleAnswer by Xedret for PHP convert XML to JSON
After researching a little bit all of the answers, I came up with a solution that worked just fine with my JavaScript functions across browsers (Including consoles / Dev Tools) :<?php // PHP Version...
View ArticleAnswer by Klesun for PHP convert XML to JSON
This solution handles namespaces, attributes, and produces consistent result with repeating elements (always in array, even if there is only one occurrence).Inspired by ratfactor's sxiToArray()./** *...
View ArticleAnswer by Atul Baldaniya for PHP convert XML to JSON
If you are ubuntu user install xml reader (i have php 5.6. if you have other please find package and install)sudo apt-get install php5.6-xmlservice apache2 restart$fileContents =...
View ArticleAnswer by Rashiqul Rony for PHP convert XML to JSON
This is better solution$fileContents= file_get_contents("https://www.feedforall.com/sample.xml");$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);$fileContents =...
View ArticleAnswer by lucifer63 for PHP convert XML to JSON
Found FTav's answer the most useful as it is very customizable, but his xml2js function has some flaws. For instance, if children elements has equal tagnames they all will be stored in a single object,...
View ArticleAnswer by Alpha for PHP convert XML to JSON
Best solution which works like a charm$fileContents= file_get_contents($url);$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);$fileContents = trim(str_replace('"', "'",...
View Article