import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
XML.toJSONObject(xml_text).toString()
org.json.XML
Answer from KonK on Stack Overflowimport 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!"
}}
Convert xml with namespaces to json in java - Stack Overflow
java - Is there a way to directly convert XML to JSON with the namespaces removed without any intermediate XML to XML transformations? - Stack Overflow
Quickest way to convert XML to JSON in Java - Stack Overflow
java - Convert XML to JSON format - Stack Overflow
You could try the XmlMapper from jackson (com.fasterxml.jackson.dataformat.xml.XmlMapper)
XmlMapper xmlMapper = new XmlMapper();
JsonNode jsonNode = xmlMapper.readTree(string.getBytes());
ObjectMapper objectMapper = new ObjectMapper();
String value = objectMapper.writeValueAsString(jsonNode);
edit: Dependencies I've used
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.0</version>
</dependency>
This question is a bit old but I would like to share my answer about the same task that I did recently! The requirement is: Having this xml:
<ns2:testplan>
<ns2:snapshot>
<ns2:revision>294</ns2:revision>
</ns2:snapshot>
<ns2:webId>70</ns2:webId>
<ns2:title>Demo test plan 06</ns2:title>
</ns2:testplan>
We would like to convert it to:
{
"ns2:testplan": {
"ns2:snapshot": {
"ns2:revision": 294
},
"ns2:title": "Demo test plan 06",
"ns2:webId": 70
}
}
Note that with the accepted response the root tag <ns2:testplan> and the namespace prefix are not included in the JSON.
To configure XmlMapper to keep the namespace and add the root tag, proceed like this:
var module = new SimpleModule().addDeserializer(JsonNode.class, new JsonNodeDeserializer() {
@Override
public JsonNode deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return ctxt.getNodeFactory()
.objectNode()
.set("ns2:testplan", super.deserialize(p, ctxt)); // this config adds the root tag
}
});
XMLInputFactory xmlInputFactory = new WstxInputFactory();
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false); // this one keeps the namespace prefix
XmlMapper xmlMapper = new XmlMapper(new XmlFactory(xmlInputFactory, new WstxOutputFactory()));
xmlMapper.registerModule(module);
Then use the code of the accepted answer to convert the XML to JSON.
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"
}}
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);
You may take a look at the Json-lib Java library, that provides XML-to-JSON conversion.
String xml = "<hello><test>1.2</test><test2>123</test2></hello>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read( xml );
If you need the root tag too, simply add an outer dummy tag:
String xml = "<hello><test>1.2</test><test2>123</test2></hello>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read("<x>" + xml + "</x>");
There is no direct mapping between XML and JSON; XML carries with it type information (each element has a name) as well as namespacing. Therefore, unless each JSON object has type information embedded, the conversion is going to be lossy.
But that doesn't necessarily matter. What does matter is that the consumer of the JSON knows the data contract. For example, given this XML:
<books>
<book author="Jimbo Jones" title="Bar Baz">
<summary>Foo</summary>
</book>
<book title="Don't Care" author="Fake Person">
<summary>Dummy Data</summary>
</book>
</books>
You could convert it to this:
{
"books": [
{ "author": "Jimbo Jones", "title": "Bar Baz", "summary": "Foo" },
{ "author": "Fake Person", "title": "Don't Care", "summary": "Dummy Data" },
]
}
And the consumer wouldn't need to know that each object in the books collection was a book object.
Edit:
If you have an XML Schema for the XML and are using .NET, you can generate classes from the schema using xsd.exe. Then, you could parse the source XML into objects of these classes, then use a DataContractJsonSerializer to serialize the classes as JSON.
If you don't have a schema, it will be hard getting around manually defining your JSON format yourself.