Quantcast
Channel: PHP convert XML to JSON - Stack Overflow
Browsing index pages (24 articles)

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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Image may be NSFW.
Clik here to view.

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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
Browsing index pages (24 articles)


Latest Images