Skip to content

Instantly share code, notes, and snippets.

@zuch
Last active Dec 25, 2020
Embed
What would you like to do?
java - Convert JSON to XML
{
"employees": [
{
"firstName": "John",
"lastName": "Doe"
},
{
"firstName": "Anna",
"lastName": "Smith"
},
{
"firstName": "Peter",
"lastName": "Jones"
}
]
}
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import org.json.JSONException;//http://bit.ly/12O4D2w
public class JSONtoXML
{
private static String InputPath = "./large.json";
private static String OutputPath = "./output.xml";
public static void main(String[] args) throws FileNotFoundException, IOException, JSONException
{
//Read JSON File
long startTime = System.currentTimeMillis();
String json = readFile(InputPath);//Read File
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("Read File Duration: "+duration);
//Convert JSON to XML
startTime = System.currentTimeMillis();
String xml = convert(json, "root");//State name of root element tag
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("Process Data Duration: "+duration);
//Write XML File
startTime = System.currentTimeMillis();
writeFile(OutputPath, xml);
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("Write File Duration: "+duration);
}
public static String convert(String json, String root) throws JSONException
{
org.json.JSONObject jsonFileObject = new org.json.JSONObject(json);
String xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n<"+root+">"
+ org.json.XML.toString(jsonFileObject) + "</"+root+">";
return xml;
}
public static String readFile(String filepath) throws FileNotFoundException, IOException
{
StringBuilder sb = new StringBuilder();
InputStream in = new FileInputStream(InputPath);
Charset encoding = Charset.defaultCharset();
Reader reader = new InputStreamReader(in, encoding);
int r = 0;
while ((r = reader.read()) != -1)//Note! use read() rather than readLine()
//Can process much larger files with read()
{
char ch = (char) r;
sb.append(ch);
}
in.close();
reader.close();
return sb.toString();
}
public static void writeFile(String filepath, String output) throws FileNotFoundException, IOException
{
FileWriter ofstream = new FileWriter(filepath);
try (BufferedWriter out = new BufferedWriter(ofstream)) {
out.write(output);
}
}
}
<?xml version="1.0" encoding="ISO-8859-15"?>
<root>
<employees>
<lastName>Doe</lastName>
<firstName>John</firstName>
</employees>
<employees>
<lastName>Smith</lastName>
<firstName>Anna</firstName>
</employees>
<employees>
<lastName>Jones</lastName>
<firstName>Peter</firstName>
</employees>
</root>
@MarappPrashanth

This comment has been minimized.

Copy link

@MarappPrashanth MarappPrashanth commented Apr 12, 2017

good one but how to write if i need to get output with attribute and values

            <Shift FridayValid="" MondayValid="" SaturdayValid=""
                ShiftEndTime="" ShiftName="" ShiftStartTime=""
                SundayValid="" ThursdayValid="" TuesdayValid="" WednesdayValid=""/>
        </Shifts>
    </EffectivePeriod>
</EffectivePeriods>
@NAWAZ4265

This comment has been minimized.

Copy link

@NAWAZ4265 NAWAZ4265 commented Dec 16, 2017

Sir, can you edit this code. So that this code can convert json to csv.

@MohammedAbdulNafay

This comment has been minimized.

Copy link

@MohammedAbdulNafay MohammedAbdulNafay commented Aug 31, 2018

Hi @MarappPrashanth, may I know if you have found any solution for your question

@javadev

This comment has been minimized.

Copy link

@javadev javadev commented Dec 25, 2020

You may use underscore-java library, method U.jsonToXml(json).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment