Json & Array from XML in 3 lines:
$xml = simplexml_load_string($xml_string);
$json = json_encode(
array = json_decode($json,TRUE);
Answer from Antonio Max on Stack Overflow Top answer 1 of 16
565
Json & Array from XML in 3 lines:
$xml = simplexml_load_string($xml_string);
$json = json_encode(
array = json_decode($json,TRUE);
2 of 16
42
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 well.
http://lostechies.com/seanbiefeld/2011/10/21/simple-xml-to-json-with-php/
<?php
class XmlToJson {
public function Parse ($url) {
$fileContents= file_get_contents(
fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
return $json;
}
}
?>
GitHub
github.com › tamlyn › xml2json
GitHub - tamlyn/xml2json: XML to JSON converter in PHP
$xmlNode = simplexml_load_file('example.xml'); $arrayData = xmlToArray($xmlNode); echo json_encode($arrayData);
Starred by 49 users
Forked by 34 users
Languages PHP 100.0% | PHP 100.0%
Videos
Lostechies
lostechies.com › seanbiefeld › 2011 › 10 › 21 › simple-xml-to-json-with-php
Simple XML to JSON with PHP · Los Techies
Just for fun we’ll point to the XML for the NFL scorestrip. <?php include 'XmlToJson.php'; print XmlToJson::Parse("http://www.nfl.com/liveupdate/scorestrip/ss.xml"); ?> Now we can call our new file, say we name it getNflDataAsJson.php, it will return the converted JSON.
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php xml to json
PHP XML to JSON | Steps to Convert XML to JSON in PHP with Examples
April 4, 2023 - In order to convert XML to JSON in PHP, we have a function called json_encode function, and this is an inbuilt function in PHP and the procedure to convert XML to JSON is first getting the contents of the XML file by making use of the function ...
Call +917738666252
Address Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
SitePoint
sitepoint.com › blog › javascript › how to create an xml to json proxy server in php
How to Create an XML to JSON Proxy Server in PHP — SitePoint
November 6, 2024 - This server can process data, translating XML messages to JSON before they reach your JavaScript code. The PHP script for the proxy server uses PHP’s cURL library to fetch content from the URL and pass it to a string. It then loads the returned string as a SimpleXMLElement object and returns a JSON-encoded message.
Aspose
products.aspose.com › aspose.cells › php via java › conversion › xml to json
PHP XML to JSON - XML to JSON Converter | products.aspose.com
November 13, 2025 - Add a library reference (import the library) to your PHP project. Load XML file with an instance of Workbook. Convert XML to JSON by calling save method of Workbook.
GitHub
github.com › markwilson › xml-to-json
GitHub - markwilson/xml-to-json: XML to JSON convert in PHP
Basic XML to JSON conversion. ... <?php use MarkWilson\XmlToJson\XmlToJsonConverter; $xml = new \SimpleXMLElement('<Element>Value</Element>'); $converter = new XmlToJsonConverter(); $json = $converter->convert($xml);
Author markwilson
Laracasts
laracasts.com › discuss › channels › general-discussion › converting-xml-to-jsonarray
Converting XML to JSON/Array
We cannot provide a description for this page right now
Top answer 1 of 3
14
Here is my variant of JSON to XML conversion. I get an array from JSON using json_decode() function:
$array = json_decode ($someJsonString, true);
Then I convert the array to XML with my arrayToXml() function:
$xml = new SimpleXMLElement('<root/>');
$this->arrayToXml($array, $xml);
Here is my arrayToXml() function:
/**
* Convert an array to XML
* @param array $array
* @param SimpleXMLElement $xml
*/
function arrayToXml($array, &$xml){
foreach ($array as $key => $value) {
if(is_int($key)){
$key = "e";
}
if(is_array($value)){
$label = $xml->addChild($key);
$this->arrayToXml($value, $label);
}
else {
$xml->addChild($key, $value);
}
}
}
2 of 3
12
Check it here: How to convert array to SimpleXML
and this documentation should help you too
Regarding Json to Array, you can use json_decode to do the same!
Codersvibe
codersvibe.com › how-to-convert-xml-to-json-in-php
How to convert XML to JSON in PHP?
July 8, 2023 - We will see, how to get XML data from XML file and convert into JSON data. So let's get started. First you need a XML file. You can create it or download XML sample file as like below. Now you can convert your XML file into JSON file. <?php $xmlObject = simplexml_load_file('sample.xml'); $jsonData = json_encode($xmlObject, JSON_PRETTY_PRINT); print_r($jsonData); ?>
Fyicenter
dev.fyicenter.com › 1000592_Convert_XML_to_JSON_with_PHP.html
Convert XML to JSON with PHP
Convert XML to JSON with PHP How to convert an XML document to a JSON text string with PHP language? Currently, there is no built-in function or any standard extension that you can use to convert an XML document to a JSON text string. But you can use the following PHP example, xml_to_json_converter.php, to convert an XML docume...
GitHub
github.com › extphp › xml-to-json
GitHub - extphp/xml-to-json: An XML to JSON converter that will properly preserve attributes
use ExtPHP\XmlToJson\JsonableXML; $xml = new JsonableXML('<node attr1="value1" attr2="value2"><child>child value</child></node>'); json_encode($xml); // convert xml to json // These methods are also available directly on the xml object.
Author extphp