Site icon TestingDocs.com

XML Parsing with JDOM

Overview

JDOM is an open-source library for Java XML parsing and data manipulations. JDOM was created to be Java-specific and thereby take advantage of Java’s features, like method overloading, collections, reflection etc.

It is available in 2 branches JDOM 2.x and JDOM 1.x . You can find the downloads, source and documentation here: http://www.jdom.org/downloads/

You can find more information about JDOM on the official website: http://www.jdom.org

JDOM library

The JDOM library contains a lot of packages.

Firstly, the org.jdom2 package holds the classes representing an XML document and its components: Attribute, Document, Element etc

org.jdom2.input package holds classes that build XML documents. The most important class is SAXBuilder. SAXBuilder builds a document by listening to incoming SAX events and building a document. It uses a SAX parser to read the stream and then builds the document according to the SAX parser callbacks.

 

 

Thirdly, org.jdom2.output package holds the classes that output XML documents. The most important class is XMLOutputter. It converts documents to a stream of bytes for output to files, streams.

The org.jdom2.transform and org.jdom2.xpath packages have classes that support built-in XSLT transformations and XPath lookup.

XML Parsing

Documents are represented by the org.jdom2.Document class. You can build a document from a file, stream,URL etc. Document class has methods that allow access to the root element as well as the other document-level information.

Document doc = new Document(new Element(“root”));

or build the document with SAX parser, using filename , url etc:

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(url);

or

SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(filename));

 

Exit mobile version