Reputation: 4289
I'm trying to convert an XML to JSON in JAVA removing the tag attributes from the XML.
I tried using org.json.XML
but it did not meet my needs.
Is there a library for doing what I want to do?
Example input:
<?xml version="1.0"?>
<company g="j">
<staff id="1001">
<firstname hi="5">jim</firstname>
<lastname>fox</lastname>
</staff>
<staff id="2001">
<firstname a="7">jay</firstname>
<details tmp="0">
<lastname>box</lastname>
<nickname >fong fong</nickname>
<salary id="99">200000</salary>
</details>
</staff>
</company>
Desired output:
{
"company": {
"staff": [
{
"firstname": "jim"
"lastname": "fox",
},
{
"firstname": "jay",
"details": {
"lastname": "box",
"nickname": "fong fong",
"salary":"200000",
}
]
}
}
I tried the following but it convert the xml using the attributes:
package my.transform.data.utils;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.XML;
import org.json.JSONObject;
public class JSONObjectConverter {
public static void main(String[] args) throws Exception {
String xml = FileUtils.readFileToString(new File("src/main/resources/staff.xml"));
JSONObject aJson = XML.toJSONObject(xml);
System.out.println(aJson.toString());
}
}
any suggestions?
Upvotes: 2
Views: 3625
Reputation: 2097
Underscore-java library has static methods U.fromXmlWithoutAttributes(string)
and U.toJson(object)
. Live example
import com.github.underscore.U;
import java.util.Map;
public class JsonConversion {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
String xmlString = "<?xml version=\"1.0\"?>"
+ "<company g=\"j\">"
+ " <staff id=\"1001\">"
+ " <firstname hi=\"5\">jim</firstname>"
+ " <lastname>fox</lastname>"
+ " </staff>"
+ " <staff id=\"2001\">"
+ " <firstname a=\"7\">jay</firstname>"
+ " <details tmp=\"0\">"
+ " <lastname>box</lastname>"
+ " <nickname >fong fong</nickname>"
+ " <salary id=\"99\">200000</salary>"
+ " </details>"
+ " </staff>"
+ "</company>";
Map<String, Object> map = (Map) U.fromXmlWithoutAttributes(xmlString);
System.out.println(U.toJson(map));
}
}
Output:
{
"company": {
"staff": [
{
"firstname": "jim",
"lastname": "fox"
},
{
"firstname": "jay",
"details": {
"lastname": "box",
"nickname": "fong fong",
"salary": "200000"
}
}
]
}
}
Upvotes: 1
Reputation: 163262
Try doing an XSLT transformation to get the XML into the desired form before conversion. (You could also consider using the XSLT 3.0 xml-to-json() function).
I think it's very likely that any general-purpose converter will do exactly what you want without pre- or post- processing.
Upvotes: 1
Reputation: 22394
You need to use JAXB to unmarshal the xml content to a java object and then use that java object to prepare the JSON.
JAXB converts the given xml to a java object (this is called unmarshalling) and then that java object can be used to form the JSON
You can refer the below code snippet:
public class JAXBToJsonConverter {
public static void main(String[] args) {
try {
//save the company details content to a .xml file
// and refer the path below
File file = new File("C:\\myproject\\company.xml");
//create the jaxb context and unmarshall
JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Company company= (Company) jaxbUnmarshaller.unmarshal(file);
//create the JSON object
JSONObject json = new JSONObject(company);
System.out.println(json);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Company class:
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Company {
private Staff staff;
@XmlElement
public Staff getStaff() {
return staff;
}
public void setStaff(Staff staff) {
this.staff = staff;
}
}
Staff class:
public class Staff {
private String firstname;
private String lastname;
@XmlElement
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@XmlElement
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
Details class:
public class Details {
private String lastname;
private String nickname;
private int salary;
@XmlElement
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@XmlElement
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@XmlElement
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
I need something more dynamic, since my xml is in a different structure every time.
You can have a look at here which uses staxon
:
https://github.com/beckchr/staxon/wiki/Converting-XML-to-JSON
Upvotes: 2