Easy integration of Java and XML with Jdom

Source: Internet
Author: User
Tags new set object model requires

Overview:

Jdom is a new API function that reads, writes, and operates XML in the Java language. Jason Hunter and Brett McLaughlin publicly released its beta version. These API functions are optimized to the fullest extent in terms of intuition, simplicity, and efficiency. In the following pages, Hunter and McLaughlin describe how to use jdom to read and write an existing XML document.

Jdom is an Open-source API that describes the contents of XML documents and documents to Java programmers in a straightforward and straightforward manner. As the name suggests, Jdom is optimized for Java. Provides a low consumption method for using XML documents. Jdom users can do what they want without having to master too much XML knowledge.

Jdom can collaborate with existing XML technologies such as simple APIs for XML (SAX) and Document Object Model (DOM). However, it is not simply extracted from these APIs. Jdom has absorbed good aspects from these existing technologies and built up a new set of classes and interfaces that, in the words of a jdom user, are "these interfaces I've been looking forward to reading from the start Org.w3c.dom"; Jdom can read the contents of Sax or DOM, You can also output formats that sax or DOM can receive. This ability enables jdom to integrate well with existing systems built with SAX or DOM.

The principle of Jdom

The first and most important one is that Jdom API functions are designed to be easy to understand for Java programmers. Other XML parsing functions are designed to be language-generic (supporting the same API functions in java,c++, even JavaScript). Jdom takes advantage of the excellent features of Java, such as method overloading, recycling mechanisms, and background processing.

In order to be easy to use, these functions have to depict XML documents in the form that programmers like. For example: What is the textual content of an element that the programmer is thinking of?

<element>This is my text content</element>

In some APIs, the textual content of an element is treated only as a child node of an element. From a technical standpoint, this design requires the following code to access the element's content:

String content = element.getFirstChild().getValue();

Jdom uses an easier and simpler way to get the content of an element:

String text = element.getText();

Jdom as much as possible to reduce the programmer's workload. According to the rules of thumb, jdom should use 20% or less effort to complete 80% or more java/xml problems. This is not to say that Jdom only supports the 80% XML specification (in fact we want jdom100% to support XML specifications). The thumb rule is that some things can be added, but not necessary. These API functions should be kept simple.

Jdom's second rule is that jdom should be fast and lightweight. Incoming and executed documents should be fast and memory consumption should be as small as possible. Jdom's design is clearly guided by this principle. For example, even at the beginning, the less coordinated operation is already faster than the DOM, but it is coarser than sax. Despite this, Jdom has many advantages that sax does not have.

Do you need Jdom?

So, do you need Jdom? That's a really good question. There is already the standard of existence, why to invent a new one? The answer is that Jdom solves the problem that existing standards cannot solve.

The DOM completely describes an element tree in memory. It is a large API that is designed to manipulate almost all possible XML tasks. It must also have the same API to support different languages. Because of these limitations, it is not used to Java programmers who are accustomed to using Java features such as method overloading and simple set,get methods. Dom also requires a lot of memory and a high frequency, which makes it difficult to work with many lightweight web applications.

Sax does not create an element tree in memory, it is described in terms of how things are developed. For example, it reports each read start and end tag. This approach makes it a lightweight, fast-read API. However, this event handling is not intuitive to the Java programmer on the server side. Sax also does not support modifying XML documents and random reads.

Jdom tries to combine the advantages of DOM and sax. It is designed as a lightweight API that can be quickly executed on small memory. Jdom also supports random reads of the entire document, but it is surprising that it does not need to read the entire document into memory. This API supports future lightweight operations that read information when needed. Also, Jdom supports the modification of XML documents through standard constructors and set methods.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.