unhurried

コンピュータ関連ネタがほとんど、ときどき趣味も…

Handling XML with Java

In this post, I will introduce methods of handling XML files with Java. (Ones built in Java SE/EE only.)

DOM (Document Object Model)

  • http://www.w3.org/DOM/DOMTR
  • DOM processes XML after loading it with tree data structure.
  • You can write codes in the same way with other programming languages because the specification of API is standardized by W3C,

SAX (Simple API for XML)

  • http://www.saxproject.org
  • The XML parser invokes events (such as a beginning of an element) while reading XML from the start. An application register handlers each of the events, and pass how to process the events.
  • The specification is defined by not standards body but volunteers, but it is supported by as many programming languages as DOM. Although It is more memory efficient than DOM, It unfits the processes which needs random accesses to the XML structure.

StAX (Streaming API for XML)

  • https://www.jcp.org/en/jsr/detail?id=173
  • The XML parser invokes events (such as a beginning of an element) while reading XML from the start. An application catch these events with I/F like the "iteration", and process according to the type of the events.
  • The specification is defined in JSR 173. StAX is described as "pull type" while SAX is described as "push type".

JAXB (Java Architecture for XML Binding)

  • https://jcp.org/en/jsr/detail?id=222
  • The specification of API which interconverts between XML and Java Objects.
  • It is defined in JSR 31 (JAXB 1.0) or JSR 222 (JAXB 2.0). It is easier to learn than JAXP as JAXB specializes in data conversion.