What is the best/simplest way to read in an XML file in Java application?

There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester. You could always use the standard JDK method of getting a document : import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; […] File file = new File(“some/path”); DocumentBuilderFactory dbf … Read more

Converting JSON to XML in Java

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> … Read more

How to read and write XML files?

Here is a quick DOM example that shows how to read and write a simple xml file with its dtd: <?xml version=”1.0″ encoding=”UTF-8″ standalone=”no”?> <!DOCTYPE roles SYSTEM “roles.dtd”> <roles> <role1>User</role1> <role2>Author</role2> <role3>Admin</role3> <role4/> </roles> and the dtd: <?xml version=”1.0″ encoding=”UTF-8″?> <!ELEMENT roles (role1,role2,role3,role4)> <!ELEMENT role1 (#PCDATA)> <!ELEMENT role2 (#PCDATA)> <!ELEMENT role3 (#PCDATA)> <!ELEMENT role4 (#PCDATA)> … Read more