Java XML Parsers



Java has various XML parsers that support object type and event type standards. We can read, create, query and modify the XML documents using these APIs. APIs provide interfaces that represent the XML documents, methods to retrieve and modify the elements and attributes in XML documents.

XML Parsers

XML Parsers are the software libraries or packages that help client applications to interact with XML documents through interfaces. They are used to check the syntax of XML and validate it according to the DTD or XML schema. Parsers can be either document based or event based.

Types of XML parsers

Following are the two main types of XML parsers −

  • DOM (Document Object Model)
  • SAX (Simple API for XML)

DOM (Document Object Model)

DOM is proposed by W3C (World Wide Web Consortium). It is a tree based API and creates the entire XML document into a parse tree inside the main memory. Hence, it consumes more memory. DOM API provides interfaces to access, add and modify the documents that are independent of programming languages. DOM is suitable for small documents as it consumes more memory. We can both read and create documents using DOM APIs.

DOM parser image

SAX (Simple API for XML)

SAX is an event based API. It will not load the entire document. Instead, it loads small parts of XML file. SAX is a read-only API, we cannot create XML documents using SAX. It is used to process large XML documents as it consumes less memory.

SAX parser image

Java XML Parsers

The JAXP (Java API for XML Processing) APIs provide the standard interfaces to process XML documents in Java applications. It has interfaces that support both DOM and SAX standards.

The below table describes various XML parsers and their relative classes or interfaces in java.

Parser Description Class/Interface
DOM Parser DOM parser represents an XML file as tree structure inside main memory. DOM provides interfaces to access and modify XML documents. DocumentBuilder
SAX Parser SAX parser parses an XML document based on events and is used only for reading. The entire file is not loaded into the main memory. SaxParser
JDOM Parser JDOM parser is an open source API that supports DOM, SAX, XSLT and XPath. It integrates with both DOM and SAX. DOMBuilder, SAXBuilder, StAXEventBuilder, StAXStreamBuilder, StAXStreamWriter
StAX Parser StAX parser is a JAVA based streaming API and it is a pull-parsing model to read and write XML documents. XMLEventReader, XMLEventWriter
DOM4J Parser DOM4J parser is a java based library that uses Java Collections framework to efficiently access and modify XML documents. DOMReader, DOMWriter, SAXReader, SAXWriter
XPath Parser XPath parser parses the XML document based on expressions to access and modify nodes. XPath
Advertisements