How To Convert XML To JSON In Java ?

In this article we will see how to convert XML into JSON in Java ? Here we will use Java-JSON library to convert XML to JSON.

First add below JSON dependency in your pom.xml

Now lets convert XML to JSON.
import org.json.JSONObject;
import org.json.XML;

public class XmlToJson {

	public static void main(String[] args) {
		String xmlStr = "<student>\r\n" + 
				"	<firstName>John</firstName>\r\n" + 
				"	<id>1</id>\r\n" + 
				"	<lastName>Smith</lastName>\r\n" + 
		JSONObject json = XML.toJSONObject(xmlStr);