JSON in Java has some great resources.
Maven dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
XML.java is the class you're looking for:
import org.json.JSONObject;
import org.json.XML;
import org.json.JSONException;
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
Output is:
{"test": {
"attrib": "moretest",
"content": "Turn this to JSON"
}}
Answer from danieltalsky on Stack OverflowJSON in Java has some great resources.
Maven dependency:
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
XML.java is the class you're looking for:
import org.json.JSONObject;
import org.json.XML;
import org.json.JSONException;
public class Main {
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><test attrib=\"moretest\">Turn this to JSON</test>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
Output is:
{"test": {
"attrib": "moretest",
"content": "Turn this to JSON"
}}
To convert XML File in to JSON include the following dependency
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
and you can Download Jar from Maven Repository here. Then implement as:
String soapmessageString = "<xml>yourStringURLorFILE</xml>";
JSONObject soapDatainJsonObject = XML.toJSONObject(soapmessageString);
System.out.println(soapDatainJsonObject);
Convert xml to json with Java - Stack Overflow
Converting JSON to XML in Java - Stack Overflow
Parse large XML file to JSON files?
How to parse a XML file into a kotlin Data Class
Videos
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
XML.toJSONObject(xml_text).toString()
org.json.XML
You can Use JSON and XML Library from json.org
import org.json.JSONObject;
import org.json.XML;
import org.junit.Test;
public class XmlToJsonTest {
private static final String XML_TEXT = "<note>\n" +
"<to>Tove</to>\n" +
"<from>Jani</from>\n" +
"<heading>Reminder</heading>\n" +
"<body>Don't forget me this weekend!</body>\n" +
"</note>";
private static final int PRETTY_PRINT_INDENT_FACTOR = 4;
@Test
public void convert() {
JSONObject xmlJSONObj = XML.toJSONObject(XML_TEXT);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
}
}
Source
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Output
{"note": {
"heading": "Reminder",
"from": "Jani",
"to": "Tove",
"body": "Don't forget me this weekend!"
}}
Use the (excellent) JSON-Java library from json.org then
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
toString can take a second argument to provide the name of the XML root node.
This library is also able to convert XML to JSON using XML.toJSONObject(java.lang.String string)
Check the Javadoc
Link to the the github repository
POM
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160212</version>
</dependency>
original post updated with new links
Underscore-java library has static method U.jsonToXml(jsonstring). Live example
import com.github.underscore.U;
public class MyClass {
public static void main(String[] args) {
String json = "{\"name\":\"JSON\",\"integer\":1,\"double\":2.0,\"boolean\":true,\"nested\":{\"id\":42},\"array\":[1,2,3]}";
String xml = U.jsonToXmlMinimum(json);
System.out.println(xml);
}
}
Output:
<root>
<name>JSON</name>
<integer>1</integer>
<double>2.0</double>
<boolean>true</boolean>
<nested>
<id>42</id>
</nested>
<array>1</array>
<array>2</array>
<array>3</array>
</root>