Quantcast
Channel: PHP convert XML to JSON - Stack Overflow
Browsing all 24 articles
Browse latest View live

Answer by MOAZZAM RASOOL for PHP convert XML to JSON

//main fuction =========================== function xml2array($responce) { $doc = new DOMDocument(); $doc->loadXML($responce); $root = $doc->documentElement; $output = domNodeToArray($root);...

View Article



Answer by T.Todua for PHP convert XML to JSON

With accepted (antonio's) answer, from such source:<MyData><Level1 myRel="parent" myName="AAA"><Level2 myRel="child1" myName="BBB"><Level2 myRel="child2" myName="CCC"> ...you...

View Article

Answer by Brandon Stewart for PHP convert XML to JSON

$content = str_replace(array("\n", "\r", "\t"), '', $response); $content = trim(str_replace('"', "'", $content)); $xml = simplexml_load_string($content); $json = json_encode($xml); return...

View Article

Answer by Márcio Rossato for PHP convert XML to JSON

if you XML is a soap file, you can use this:$xmlStr = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlStr);$xml = new SimpleXMLElement($xmlStr);return json_encode($xml);

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


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

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

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 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 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 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 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 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
Browsing all 24 articles
Browse latest View live




Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>