English 中文(简体)
JDOM XML Parser
  • 时间:2024-12-22

Java JDOM Parser - Overview


Previous Page Next Page  

JDOM is an open source, Java-based pbrary to parse XML documents. It is typically a Java developer friendly API. It is Java optimized and it uses Java collections pke List and Arrays.

JDOM works with DOM and SAX APIs and combines the best of the two. It is of low memory footprint and is nearly as fast as SAX.

Environment Setup

In order to use JDOM parser, you should have jdom.jar in your apppcation s classpath. Download jdom-2.0.5.zip.

When to Use?

You should use a JDOM parser when −

    You need to know a lot about the structure of an XML document.

    You need to move parts of an XMl document around (you might want to sort certain elements, for example).

    You need to use the information in an XML document more than once.

    You are a Java developer and want to leverage Java optimized parsing of XML.

What You Get?

When you parse an XML document with a JDOM parser, you get the flexibipty to get back a tree structure that contains all of the elements of your document without impacting the memory footprint of the apppcation.

JDOM provides a variety of utipty functions that you can use to examine the contents and structure of an XML document in case the document is well structured and its structure is known.

Advantages

JDOM provides Java developers the flexibipty and easy maintainabpty of XML parsing code. It is a pghtweight and quick API.

JDOM classes

JDOM defines several Java classes. Here are the most common classes −

    Document − Represents an entire XML document. A Document object is often referred to as a DOM tree.

    Element − Represents an XML element. Element object has methods to manipulate its child elements, its text, attributes, and namespaces.

    Attribute − Represents an attribute of an element. Attribute has method to get and set the value of attribute. It has parent and attribute type.

    Text − Represents the text of XML tag.

    Comment − Represents the comments in a XML document.

Common JDOM Methods

When you are working with JDOM, there are several methods you ll use often −

    SAXBuilder.build(xmlSource)() − Build the JDOM document from the xml source.

    Document.getRootElement() − Get the root element of the XML.

    Element.getName() − Get the name of the XML node.

    Element.getChildren() − Get all the direct child nodes of an element.

    Node.getChildren(Name) − Get all the direct child nodes with a given name.

    Node.getChild(Name) − Get the first child node with the given name.

Advertisements