Skip to content

Instantly share code, notes, and snippets.

@jsianes
Last active December 19, 2015 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsianes/5874455 to your computer and use it in GitHub Desktop.
Save jsianes/5874455 to your computer and use it in GitHub Desktop.
A short Java program that takes any XML file from input and generates a JSON compliant document
import java.io.*;
import java.util.*;
import org.json.*;
/*
JSON library provided by json.org (http://www.json.org/)
*/
public class XmltoJson {
public static void main(String[] args) {
int i=0,size=args.length;
try {
while (i<size) {
String XMLString="";
XMLString = new Scanner( new File(args[i]),"UTF-8").useDelimiter("\\A").next();
JSONObject jsonObj = null;
jsonObj = XML.toJSONObject(XMLString);
String json = jsonObj.toString();
if (!json.equals("{}")) { System.out.println(json); }
i++;
}
} catch (FileNotFoundException e) {
} catch (JSONException e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment