Easily integrate Java and XML with JDOM

xiaoxiao2021-03-06  43

Overview: JDOM is read, write, write, and operate XML new API functions. Jason Hunter and Brett McLauhlin disclose its test version. These API functions are maximized optimized in the premise of intuition, simple and efficient. In the next parameters, Hunter and McLaughlin introduces how to read a already existing XML document with Jdom. JDOM is an open source API that describes the contents of XML documents and documents to Java programmers in a direct easy-to understand. As the name is revealed, JDOM is optimized for Java. Provide a low consumption method for using an XML document. JDOM users can do the desired operations without having to master too much XML knowledge. JDOM can cooperate with existing XML technology such as Simple API for XML (SAX) and Document Object Model (DOM). However, it is not simple from these APIs. JDom has absorbed good aspects from these existing technologies, and has established a new set of new classes and interfaces. It is: "These interfaces are what I read from the beginning Org.W3c.dom look forward to ""; JDOM can read the SAX or the DOM content, or output SAX or DOM can receive the format. This capability can make JDOM good and existing systems established with SAX or DOM. JDOM's principle first and the most important one is that the JDOM's API function is designed to be easy to understand for Java programmers. Other XML parses functions are designed to be universal (support the same API function in Java, C , and even in JavaScript). JDOM uses Java's excellent features such as method overload, recycling mechanism, and background processing. In order to be easily easy, these functions have to depict the XML document in the form of programmers like. For example: What is the text content of a programmer's text?

this is my text content

In some APIs, the text content of the element is only used as a child node of an element. From a technical point of view, this design requires the following code to access the element:

String content = element.getfirstchild (). GetValue ();

JDOM uses a simpler and easy way to get the content of the element:

String text = element.getText ();

JDM reduces the workload of programmers as much as possible. According to the thumb rules, JDOM should use 20% or less efforts to complete 80% or more Java / XML issues. This is not to say that JDOM just supports 80% XML specification (in fact we want JDOM100% support XML specification). This thumb rule is that some things can be added, but there is no need. These API functions should be concise. The second principle of JDOM is that JDOM should be fast and lightweight. The transfer and execution documents should be fast, and the memory consumption should be as small as possible. JDOM design is obvious to follow this principle. For example, even if it starts, the operation of less coordinated operation is fast than DOM, but is rough than SAX. Despite this, JDOM still has many SAX nothing.

Do you need jdom? So, do you need JDOM? This is really a good problem. Already have a standard, why is it going to invent a new one? The answer is that JDOM solves the problem of existing standards. The DOM is fully described in memory. It is a big API that is designed to operate almost all possible XML tasks. It must also have the same API to support different languages. Because of these restrictions, it is very uncomfortable for those habitual use of Java, such as method overload, simple set, GET method's Java programmer. DOM also requires a lot of memory and higher frequency, which makes it difficult to work with many lightweight web applications. SAX did not build an element tree in memory, which is described in the way of developing. For example: it reports each read start tag and end tag. This processing method makes it a lightweight quick read API. However, this event handling method is not intuitive for the server-side Java programmers. SAX also does not support modifying XML documents and random read. Jdom attempts to combine the advantages of DOM and SAX. It is designed to quickly perform a lightweight API in small memory. JDOM also supports random reading of the entire document, but amazing is that it does not need to read the entire document in memory. This API supports the secondary lightweight operation of the information when needed. Also, JDOM supports the modification of XML documents through standard constructors and SET methods. Get an XML document JDOM to describe an XML document with an instance of the org.jdom.Document class. This document class is a lightweight level, which can include document types, objects, one root elements, and annotation objects for multiple processing instructions. You can construct a document from the draft without a constructor. Document Doc = New Document ("RootElement")); how easy we will discuss with drafts to construct an XML document from drafts. But now, we construct our document from an existing file, a stream, or a URL path.

SAXBUILDER Builder = New Saxbuilder (); Document Doc = Builder.build (URL); You can construct a document from any data source with the constructor provided in the org.jdom.input package. Recently, there are two construction methods: SAXBUILDER and DOMBUILDER. SaxBuilder constructs a document from the file with the SAX parser. SAXBuilder listens for the SAX event and creates a corresponding document from memory. This method is very fast (basically as fast as SAX), which is also our recommended way. DomBuilder is another optional way that creates a JDOM document from an existing Org.W3c.Dom.Document object. It allows JDOM to easily implement interfaces for tools to build DOM trees. JDOM speed has the improved potential to increase the completion of a delayed constructor. This constructor checks the XML data source, but parsing it when the request is requested. For example: the attribute of the document does not need to be parsed when not access. The constructor is still developing, which can make a JDOM documentation through SQL query, LDAP query, and other data formats. So, once you enter memory, the document is not related to the tools to build it. SAXBUILDER and DOMBUILDER constructors allow users to indicate whether they should be rotated to determine which parser time performs parsing tasks.

转载请注明原文地址:https://www.9cbs.com/read-79525.html

New Post(0)