Wednesday, July 27, 2016

Java XML Parsers

What is XML Parsing?
Parsing XML refers to going through XML document to access data or to modify data in one or other way.

What is XML Parser?
XML Parser provides way how to access or modify data present in an XML document. Java provides multiple options to parse XML document. Following are various types of parsers which are commonly used to parse XML documents.
Dom Parser - Parses the document by loading the complete contents of the document and creating its complete hierarchical tree in memory.
When to use?
You should use a DOM parser when:
·         You need to know a lot about the structure of a document
·         You need to move parts of the document around (you might want to sort certain elements, for example)
·         You need to use the information in the document more than once

SAX Parser - Parses the document on event based triggers. Does not load the complete document into the memory.
When to use?
You should use a SAX parser when:
·         You can process the XML document in a linear fashion from the top down
·         The document is not deeply nested
·         You are processing a very large XML document whose DOM tree would consume too much memory.Typical DOM implementations use ten bytes of memory to represent one byte of XML
·         The problem to be solved involves only part of the XML document
·         Data is available as soon as it is seen by the parser, so SAX works well for an XML document that arrives over a stream
Disadvantages of SAX
·         We have no random access to an XML document since it is processed in a forward-only manner
·         If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own


JDOM Parser - Parses the document in similar fashion to DOM parser but in more easier way.
When to use?
You should use a JDOM parser when:
·         You need to know a lot about the structure of a document
·         You need to move parts of the document around (you might want to sort certain elements, for example)
·         You need to use the information in the document more than once
·         You are a java developer and want to leverage java optimized parsing of XML.

Advantages

JDOM gives java developers flexibility and easy maintainablity of xml parsing code. It is light weight and quick API.

 StAX Parser - Parses the document in similar fashion to SAX parser but in more efficient way.
When to use?
You should use a StAX parser when:
·         You can process the XML document in a linear fashion from the top down.
·         The document is not deeply nested.
·         You are processing a very large XML document whose DOM tree would consume too much memory. Typical DOM implementations use ten bytes of memory to represent one byte of XML.
·         The problem to be solved involves only part of the XML document.
·         Data is available as soon as it is seen by the parser, so StAX works well for an XML document that arrives over a stream.
Disadvantages of SAX
·         We have no random access to an XML document since it is processed in a forward-only manner
·         If you need to keep track of data the parser has seen or change the order of items, you must write the code and store the data on your own

XPath Parser - Parses the XML based on expression and is used extensively in conjunction with XSLT.

DOM4J Parser - A java library to parse XML, XPath and XSLT using Java Collections  Framework, provides support for DOM, SAX and JAXP.
When to use?
You should use a DOM4J parser when:
You need to know a lot about the structure of a document
You need to move parts of the document around (you might want to sort certain elements, for example)
·         You need to use the information in the document more than once
·         You are a java developer and want to leverage java optimized parsing of XML.



No comments:

Post a Comment