Java XML - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java XML JDOM

JJDOM XML

shape Description

JDOM stands for Java-based Document Object Model for XML. JDOM is a tree-based API used to process XML documents with Java. Like the DOM, JDOM characterizes a XML archive as a tree comprising of components, qualities, content hubs handling directions, remarks. The tree created will be available haphazardly whenever. Rather than utilizing interfaces to speak to tree hubs, solid classes are utilized by JDOM XML Parser. Besides, there is no non specific Node interface or class which all the distinctive hub classes execute or broaden.

When to use?

A JDOM parser should be used:
  • When the document structure is huge
  • If required to replace the document elements around
  • If required to use document data multiple times.
When JDOM parser parses the XML document, all the elements in the tree structure can be retained back without effecting the application memory. The JDOM allows to examine the contents and structure of the document with the help of utility functions. This is preferable only if document is well structured.

shape Example

Below is the example of JDOM parser which parses a XML document. Step-1: Initially, the packages that are related to XML are imported. [java] import java.io.*; import java.util.*; import org.jdom2.*; [/java] Step-2: Then create the above mentioned SAXBuilder class. [java]SAXBuilder saxBuilder = new SAXBuilder();[/java] Step-3: A XML document should be created from file or a stream. [java]File inputFile = new File("input.txt"); SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build(inputFile); [/java] Step-4: Here, the root element have to be extracted. [java]Element classElement = document.getRootElement();[/java] Step-5: The attributes and sub-elements are tested. [java] //returns specific attribute getAttribute("attributeName"); [/java] [java] //returns a list of subelements of specified name getChildren("subelementName"); //returns a list of all child nodes getChildren(); //returns first child node getChild("subelementName"); [/java] If kept all the above steps at once, below is the input XML document that has to be parsed. [xml] <?xml version="1.0"?> <class> <student rollno="393"> <firstname>John</firstname> <lastname>Mike</lastname> <nickname>Jom</nickname> <marks>85</marks> </student> <student rollno="493"> <firstname>Rafeal</firstname> <lastname>Nadal</lastname> <nickname>Rafa</nickname> <marks>95</marks> </student> <student rollno="593"> <firstname>Samuel</firstname> <lastname>Johnson</lastname> <nickname>Sam</nickname> <marks>90</marks> </student> </class> [/xml] DomParserDemo.java: [java] import java.io.File; import java.io.IOException; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; public class JDomParserDemo { public static void main(String[] args) { try { File inputFile = new File("input.txt"); //SAXBuilder.build(xmlSource)() - Build the JDOM document from the xml source. SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build(inputFile); //Document.getRootElement() - Get the root element of the XML System.out.println("Root element :" + document.getRootElement().getName()); Element classElement = document.getRootElement(); List<Element> studentList = classElement.getChildren(); System.out.println("----------------------------"); for (int temp = 0; temp < studentList.size(); temp++) { Element student = studentList.get(temp); System.out.println("\nCurrent Element :" + student.getName()); Attribute attribute = student.getAttribute("rollno"); System.out.println("Student roll no : " + attribute.getValue() ); //Node.getChild(Name) - Get first child node with given name. System.out.println("First Name : " + student.getChild("firstname").getText()); System.out.println("Last Name : "+ student.getChild("lastname").getText()); System.out.println("Nick Name : "+ student.getChild("nickname").getText()); System.out.println("Marks : "+ student.getChild("marks").getText()); } }catch(JDOMException e){ e.printStackTrace(); }catch(IOException ioe){ ioe.printStackTrace(); } } } [/java] JDOM relies on upon a SAX parser as it could exclude the parser itself. This should be possible with a custom ContentHandler to parse records and assemble JDOM models from them. JDOM can likewise change over DOM Document objects into JDOM Document objects. After fulfillment of working with a record in memory, JDOM permits to serialize the report back onto the stream as a succession of bytes. JDOM offers choices to indicate the encoding, indenting, line end characters, and different subtle elements of serialization. SAXbuilder utilizes an outsider SAX parser to handle the parsing obligations and utilizations an occurrence of a SAXHandler to listen to the SAX occasions keeping in mind the end goal to develop an archive with JDOM content utilizing a JDOMFactory. Output: [java] Root element :class ---------------------------- Current Element :student Student roll no : 393 First Name : John Last Name : Mike Nick Name : Jom Marks : 85 Current Element :student Student roll no : 493 First Name : Rafeal Last Name : Nadal Nick Name : Rafa Marks : 95 Current Element :student Student roll no : 593 First Name : Samuel Last Name : Johnson Nick Name : Sam Marks : 90 [/java]

Summary

shape Key Points

  • JDOM stands for Java-based Document Object Model for XML.
  • JDOM allows to serialize the document back onto the stream as a sequence of bytes.
  • JDOM is flexible and easy to maintain.