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

Java XML Overview

Java XML

shape Description

If the XML file data has to be read then developer has to write their own code to parse the XML file which is complicated procedure. To avoid this, Java Programming Language has given parsers like DOM and SAX to read the XML file. XML Parser consumes lot of time and it is complicated. Whereas DOM/SAX parser is used to parse the XML file. Using this, the code can be developed fast and accurate.

Types of XML Processors

shape Types

Here are the different types of XML Processors in Java. Typically, they break down into 3 categories:

Tree Based Processor

A Tree-based XML processor represents the entire XML document as a tree of objects in memory which gives a lot of convenience. The tree can be traversed forward or backward, inspect one part of the tree and then jump to another part of the tree easily. The main advantage with tree based processor is that the user can search the XML content with XPATH expression language or with tools that are specific to a particular API. But, the downside of tree-based processor is that it takes more memory and certain tasks can be a lot slower than streaming API. Eg: DOM, JDOM

Streaming Processor

Streaming Processors are designed to build or parse XML one node at a time. There are two kinds of streaming processors known as "pull" and "push" processors. Eg: Simple API for XML(SAX) is a streaming processor. That means it pushes the data into call back methods that are designed. In contrast, this Streaming API for XML(StAX) is a pull process where only data is looked and only call methods that are meaningful. Typically, pull processors are more convenient programming model but both types of streaming processors can be incredibly fast and highly-memory efficient. The downside of the streaming porcessor is that the complete dataset is not stored in memory at once. XPath style search is not applicable and also coating can be complex. Especially for SAX.

Binding-style Processor

Binding Processors are similar to DOM in the background i.e. their tree style processors that store all the data in the memory all at the same time. But, the programming model is dramatically different to use. A binding processor like JAXB or Simple XML Serialization framework annotates the Java classes indicating which properties or fields have that class are mapped to the portions of XML structure and then runs the very simple code either serialize of deserialize the XML content. The upside of binding processor is that it is very efficient programming model and is very easy to maintain and the downside is that the JAXB processor is included with Oracle's JDK is not available in Android. But, there is a binding processor that works with the android called as Simple XML Serialization Framewrok. Eg: JAXB, Simple XML Serialization framework

shape Example

Basic example of XML with root element as Class which has the child Name and Sections. Sections in turn has the sub-child's Section with Name and Student Names. [xml] <?xml version="1.0"?> <Class> <Name>First</Name> <Sections> <Section> <Name>A</Name> <Students> <Student>John</Student> <Student>Mike</Student> <Student>Sohan</Student> <Student>Louis</Student> <Student>Vettori</Student> </Students> </Section> <Section> <Name>B</Name> <Students> <Student>Robert</Student> <Student>Julie</Student> <Student>Kalie</Student> <Student>Michael</Student> </Students> </Section> </Sections> </Class> [/xml]

Summary

shape Key Points

  • Java XML is used to parse the XML files in fast and accurate way.
  • A Tree-based XML processor represents the entire XML document as a tree of objects.
  • Streaming Processors are designed to build or parse XML one node at a time.
  • Binding processor is very efficient programming model and is very easy to maintain.