Easy integration of Java and XML with Jdom

Source: Internet
Author: User
Tags constructor functions implement include ldap new set object model requires
Dom|xml 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 are 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.

Get XML document

Jdom uses an instance of the Org.Jdom.Document class to describe an XML document. This document class is lightweight and can include document types, objects with multiple processing instructions, a root element, and annotation objects. You can construct a document from a draft without the need for a builder.

Document doc = new Document (New Element ("RootElement"));

Later in this article, we'll discuss how easy it is to construct an XML document from scratch. But now, we construct our documents from an existing file, a stream, or a URL path.

Saxbuilder builder = new Saxbuilder ();
Document doc = builder.build (URL);

You can use Org. The constructor class provided in the Jdom.input package constructs the document from any data source. Recently, there are two ways of constructing: Saxbuilder and Dombuilder. Saxbuilder constructs a document from a file using a SAX parser. Saxbuilder listens for Sax events and creates a corresponding document from memory. This approach is very fast (almost as fast as sax), and that's the way we recommend it. Dombuilder is an alternative way to create jdom documents from an existing Org.w3c.dom.Document object. It allows jdom to easily implement interfaces with tools that build dom trees.

The speed of the jdom has the potential to be expected to improve through the completion of an extended constructor. This constructor examines the XML data source, but resolves it when requested. For example, a document's properties do not need to be parsed when they are not accessed.

The constructor is still evolving, making it possible to make jdom documents through SQL queries, LDAP queries, and other data formats. So, once in memory, the document has nothing to do with the tool that built it.

The Saxbuilder and Dombuilder constructors allow users to indicate whether they should rotate in order to determine which parser time performs the resolved task.

Public Saxbuilder (String Parserclass, boolean validation);
Public Dombuilder (String Adapterclass, boolean validation);

The default is to use the Apache open source Xerces parser and turn off the rotation. You should note that Dombuilder is not like an analytic class, but rather like an adaptive class. This is because not all DOM parsers are the same API. In order to still allow the user to choose a preferred parser, Jdom uses an adapter class that applies a common API to all DOM parsers. This adapter class supports all popular DOM parsers, including Apache Xerces, CRIMSON,IBM's xml4j, Sun's Project X, and Oracle's parsers V1 and V2. Each parser through the correct invocation of the other parser method to perform a standard interface. This is a bit like JAXP, except that it supports a new parser that JAXP does not support.

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.

Get XML document

Jdom uses an instance of the Org.Jdom.Document class to describe an XML document. This document class is lightweight and can include document types, objects with multiple processing instructions, a root element, and annotation objects. You can construct a document from a draft without the need for a builder.

Document doc = new Document (New Element ("RootElement"));

Later in this article, we'll discuss how easy it is to construct an XML document from scratch. But now, we construct our documents from an existing file, a stream, or a URL path.

Saxbuilder builder = new Saxbuilder ();
Document doc = builder.build (URL);

You can use Org. The constructor class provided in the Jdom.input package constructs the document from any data source. Recently, there are two ways of constructing: Saxbuilder and Dombuilder. Saxbuilder constructs a document from a file using a SAX parser. Saxbuilder listens for Sax events and creates a corresponding document from memory. This approach is very fast (almost as fast as sax), and that's the way we recommend it. Dombuilder is an alternative way to create jdom documents from an existing Org.w3c.dom.Document object. It allows jdom to easily implement interfaces with tools that build dom trees.

The speed of the jdom has the potential to be expected to improve through the completion of an extended constructor. This constructor examines the XML data source, but resolves it when requested. For example, a document's properties do not need to be parsed when they are not accessed.

The constructor is still evolving, making it possible to make jdom documents through SQL queries, LDAP queries, and other data formats. So, once in memory, the document has nothing to do with the tool that built it.

The Saxbuilder and Dombuilder constructors allow users to indicate whether they should rotate in order to determine which parser time performs the resolved task.

Public Saxbuilder (String Parserclass, boolean validation);
Public Dombuilder (String Adapterclass, boolean validation);

The default is to use the Apache open source Xerces parser and turn off the rotation. You should note that Dombuilder is not like an analytic class, but rather like an adaptive class. This is because not all DOM parsers are the same API. In order to still allow the user to choose a preferred parser, Jdom uses an adapter class that applies a common API to all DOM parsers. This adapter class supports all popular DOM parsers, including Apache Xerces, CRIMSON,IBM's xml4j, Sun's Project X, and Oracle's parsers V1 and V2. Each parser through the correct invocation of the other parser method to perform a standard interface. This is a bit like JAXP, except that it supports a new parser that JAXP does not support.

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.

Get XML document

Jdom uses an instance of the Org.Jdom.Document class to describe an XML document. This document class is lightweight and can include document types, objects with multiple processing instructions, a root element, and annotation objects. You can construct a document from a draft without the need for a builder.

Document doc = new Document (New Element ("RootElement"));

Later in this article, we'll discuss how easy it is to construct an XML document from scratch. But now, we construct our documents from an existing file, a stream, or a URL path.

Saxbuilder builder = new Saxbuilder ();
Document doc = builder.build (URL);

You can use Org. The constructor class provided in the Jdom.input package constructs the document from any data source. Recently, there are two ways of constructing: Saxbuilder and Dombuilder. Saxbuilder constructs a document from a file using a SAX parser. Saxbuilder listens for Sax events and creates a corresponding document from memory. This approach is very fast (almost as fast as sax), and that's the way we recommend it. Dombuilder is an alternative way to create jdom documents from an existing Org.w3c.dom.Document object. It allows jdom to easily implement interfaces with tools that build dom trees.

The speed of the jdom has the potential to be expected to improve through the completion of an extended constructor. This constructor examines the XML data source, but resolves it when requested. For example, a document's properties do not need to be parsed when they are not accessed.

The constructor is still evolving, making it possible to make jdom documents through SQL queries, LDAP queries, and other data formats. So, once in memory, the document has nothing to do with the tool that built it.

The Saxbuilder and Dombuilder constructors allow users to indicate whether they should rotate in order to determine which parser time performs the resolved task.

Public Saxbuilder (String Parserclass, boolean validation);
Public Dombuilder (String Adapterclass, boolean validation);

The default is to use the Apache open source Xerces parser and turn off the rotation. You should note that Dombuilder is not like an analytic class, but rather like an adaptive class. This is because not all DOM parsers are the same API. In order to still allow the user to choose a preferred parser, Jdom uses an adapter class that applies a common API to all DOM parsers. This adapter class supports all popular DOM parsers, including Apache Xerces, CRIMSON,IBM's xml4j, Sun's Project X, and Oracle's parsers V1 and V2. Each parser through the correct invocation of the other parser method to perform a standard interface. This is a bit like JAXP, except that it supports a new parser that JAXP does not support.



Related Article

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.