Java XML Tutorial

In Java JDK, two built-in XML parsers are available – DOM and SAX, both have their pros and cons. Here’s few examples to show how to create, modify and read a XML file with Java DOMSAXJDOM.
In addition, updated JAXB example to show you how to convert object to / from XML.

DOM XML Parser

The DOM is the easiest to use Java XML Parser. It parses an entire XML document and load it into memory, modeling it with Object for easy nodel traversal. DOM Parser is slow and consume a lot memory if it load a XML document which contains a lot of data.

SAX XML Parser

SAX parser is work differently with DOM parser, it does not load any XML document into memory and create some object representation of the XML document. Instead, the SAX parser use callback function (org.xml.sax.helpers.DefaultHandler) to informs clients of the XML document structure.
SAX Parser is faster and uses less memory than DOM parser.

JDOM XML Parser

JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. It’s an alternative to DOM and SAX.

JAXB Example

JAXB, using annotation to convert Java object to / from XML file.
  • JAXB 2.0 hello world example
    A detail example to show you use JAXB to do XML Marshalling (convert object to XML) and XML Unmarshalling (Convert XML to object).

XML & Properties

The java.util.Properties class has build-in functionality to convert properties file into XML file or vice versse.

References

  1. Wiki Java_API_for_XML_Processing
  2. DOM XML parser documentation
  3. JDOM XML parser
  4. SAX useful quick start example
  5. SAX XML parser documentation
  6. JAXB Official Website
  7. Properties documentation
  8. Processing XML with Java

Komentar