The use of the Xml:java document model in Java

Source: Internet
Author: User
Tags benchmark comparison exception handling

In the first article in this series, I studied the performance of some of the main XML document models written in Java. However, performance is only part of the problem when you start to select this type of technology. Ease of use is at least as important, and it is already a major reason to support the use of Java-specific models rather than language-independent DOM.

To actually understand which models really work, you need to know how they rank in terms of usability. In this article, I'm going to try this work, starting with the sample code, to show how to encode common types of operations in each model. The results are summarized to conclude this article, and some other factors are proposed to promote a representation that is easier to use than another.

Refer to the previous article for background information about each model used in this comparison, including the actual version number. You can also refer to the Resources section for source download, links to the Model home page, and other relevant information.

Code comparison

In these comparisons of usage techniques for different document representations, I'll show you how to implement three basic operations in each model:

Build a document from an input stream

Iterate over elements and content, and make some changes:

Removes leading and trailing white space from the text content.

If the result text content is empty, delete it.

Otherwise, it wraps to a new element named "text" in the namespace of the parent element.

Write the modified document to the output stream

The code for these examples is based on the benchmark program I used in the previous article and has been simplified. The focus of the benchmark program is to show the best performance for each model; For this article, I'll try to show the easiest way to implement the operation in each model.

I've structured the example for each model into two separate pieces of code. The first paragraph is the code that reads the document, invokes the modified code, and writes the modified document. The second paragraph is a recursive method that truly traverses the presentation of the document and executes the modification. To avoid distraction, I've ignored exception handling in my code.

Even if you don't want to use the DOM implementation, it's worth browsing for a description of DOM usage. Because the DOM example is the first example, I use it to explore more detailed information about some of the problems and structures of the example compared to the model that follows. Browsing through these can add to the details you want to know, and if you read one of the other models directly, you'll miss the details.

Dom

The DOM specification covers all types of operations represented by the document, but it does not involve issues such as parsing the document and generating text output. Includes two DOM implementations in performance testing, Xerces and crimson, which use different techniques for these operations. Listing 1 shows a form of Xerces's top-level code.

Listing 1. Xerces DOM top-level code

1 // parse the document from input stream ("in")
2 DOMParser parser = new DOMParser();
3 parser.setFeature("http://xml.org/sax/features/namespaces", true);
4 parser.parse(new InputSource(in));
5 Document doc = parser.getDocument();
6 // recursively walk and modify document
7 modifyElement(doc.getDocumentElement());
8 // write the document to output stream ("out")
9 OutputFormat format = new OutputFormat(doc);
10 XMLSerializer serializer = new XMLSerializer(out, format);
11 serializer.serialize(doc.getDocumentElement());

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.