Wednesday, June 8, 2022

The Java XML module

The Java XML module contains APIs for DOM, StaX, and SAX. The later two only really exist to enable you to handle performance issues. SAX and StaX can save you from having to create the entire DOM tree in memory, but the DOM is clearly the more powerful of the group. You may be familiar with the DOM from other programming languages, since its an open standard.

After the modularization of the JDK all the Java XML packages were moved into the XML module, except for the XML cryptography packages which are part of the XML security API. With the modularisation of the JDK, the XML module is now the third largest module after the base module and the desktop module. It is quite vast.

Also included in this is the JAXP. The JAXP (Java API for XML processing) can be seen as an abstraction layer. It does not provide a new means of parsing XML or add to existing APIS like DOM, StaX, or SAX but it makes some things easier to deal with. So it is included in the Java XML module in Java SE.

Parsing an XML document:
First I will create an XML document:
<history>
 <era>
   <event start="100" end="200"></event>
   <event start="400" end="500"></event>
 </era>

 <era>
  <event start="1200" end="1300"></event>
  <event start="1800" end="1900"></event>
 </era>
</history>
Storing this as history.xml then getting the DOM tree from this XML document is as simple as a couple lines of Java code:
var file = new File(System.getProperty("user.home") + "/Documents/history.xml");
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = dBuilder.parse(file);

At the moment I won't do anything interesting with the DOM tree, but as you can imagine by the sheer scale and scope of the Java XML package there is a lot that you can do including calling transformations with XSLT.

The heavy amount of XML technologies in Java SE is a consequence of the considerable success that XML has recieved in the enterprise. Much like Java, XML plays a central role in the enterprise. Of course, you can also process XML using Clojure which is worth looking in to as well. By considering XML we are one step closing to an overview of Java SE.

External links:
Java XML

No comments:

Post a Comment