JDOM/XPath programming guide

Source: Internet
Author: User
Tags processing instruction xquery
JDOM/XPath programming guide

Document options

<Tr valign = "TOP"> <TD width = "8"> </TD> <TD width = "16"> </ TD> <TD class = "small" width = "122"> <p> <SPAN class = "Ast"> the required JavaScript document options are not displayed </span> </P> </TD> </tr>


Send this page as an email

Level: elementary

Xue Gu Yu (rainight@126.com), Senior Java engineer, nordsan Information Technology Development Co., Ltd.

May 01, 2004

This article introduces JDOM and XPath respectively, and the benefits of XML programming by combining them.

Preface

XML is an excellent form of data packaging and data exchange. XML is widely used in the world today. If you have never heard of its name, it is really ignorant. The advantage of using XML to describe data is obvious. It has the advantages of simple structure, facilitating reading by humans and machines, and makes up for the deficiency of Relational Data's ability to describe real data in the objective world. Based on the needs of the technical field, W3C has developed XML format rules and established a descriptive model, dom for short. Various popular programming languages have launched their own XML Parser based on this model. In the Java World, xerces developed by Apache should be one of the most popular and powerful XML parser. However, since W3C is not designed for a specific language when designing the DOM model, it adds a lot of tedious and unnecessary details for versatility, this makes it inconvenient for Java programmers to develop XML applications. Therefore, JDOM, as a new type of XML parser, is born without following the DOM model, we have built our own independent JDOM model (note that JDOM is by no means a DOM extension, although its name is similar, but the two are in a parallel relationship) and provide a powerful and convenient class library, java programmers can develop their own XML applications more efficiently and greatly reduce the amount of code, so it is quickly recognized by the industry, major aircraft carrier-level products such as JBuilder use JDOM as the XML analysis engine.

With the description standard of XML data, people naturally think of a query language that can search for data from any node in XML, just as SQL statements can execute query operations in relational databases, XQuery and XPath conform to the trend and come into being. Because XQuery is complex and inconvenient to use, XPath gradually becomes the mainstream. We only need to learn XPath to meet all query requirements. The latest version v1.0bata10 released by JDOM has already added support for xpath, which is undoubtedly very exciting for developers.

By learning JDOM and XPath, you will no longer be XML writers. In your future development career, it will be like the multi-purpose daggers of Special Forces, helping you go forward. We need to learn from the ground and start from scratch.



Back to Top

XPath Quick Start

XPath follows the path format of the Document Object Model (DOM). Since each XML document can be viewed as a tree with many nodes, each node can be one of the following seven types: root, element, attribute, text, namespace, processing instruction, and comment ). The basic syntax of xpath is composed of expressions. An object is generated after the expression value is calculated. This object has four basic types: node set, Boolean, numeric, and string. XPath is basically similar to searching for files in the file system. If the path starts with "/", it indicates that the path represents an absolute path, this is consistent with the definition of file paths in UNIX systems. Starting with "//", it indicates searching anywhere in the document.

Without talking about general theories, learning XPath is also the fastest way to learn from instances, and it helps you to put it apart.

The following XML document describes the basic information of a hard disk in an electric brain (the root node <HD> represents the hard disk, and the <disk> tag represents the hard disk partition, the name attribute shows two partitions named "C" and "D". Each partition contains <capacity>, <directories> <files> three nodes represent the partition space size, directory quantity, and number of contained files respectively ):

<?xml version="1.0" encoding="UTF-8"?>
<HD>
<disk name="C">
<capacity>8G</capacity>
<directories>200</directories>
<files>1580</files>
</disk>
<disk name="D">
<capacity>10G</capacity>
<directories>500</directories>
<files>3000</files>
</disk>
</HD>

You can use location path expressions in XML documents to search for information. These expressions are composed of many methods.

Node element search is the most frequent query method you will encounter. In the preceding XML document, the root HD contains the disk node. You can use the path to find these nodes, and use a forward slash (/) to separate the child nodes and return all elements that match the pattern. The following XPath statement returns all disk elements:

/HD/Disk

"*" Indicates "all. /HD/* indicates all nodes in HD.

The following XPath returns all nodes named "disk" under any node:

// Disk

The following XPath will return all nodes whose names are disk and whose names are 'C:

/HD/disk [@ name = 'C']

The additional elements of a node, such as attributes and functions, must be expanded in square brackets. The @ sign must be added before the attribute.

The following XPath will return a 1580 file node:

/HD/Disk/files [text () = '000000']

We have noticed that the preceding text () is a function of XPath, which extracts the text of the current node.

The following XPath returns a partition with 1580 objects:

/HD/Disk/files [text () = '000000']/parent ::*

The last parent: * indicates the set of all parent nodes of this element.

Some useful functions in XPath:

String Concat (String, String, string *) Join two strings
Boolean Starts- (String, string) Determines whether a string starts with another string.
Boolean Contains (String, string) Determines whether a string contains another string.
String Substring (String, number, number) Substring
Number String-length (String) Test String Length
Number Sum (Node-set) Sum
Number Floor (Number) Returns the maximum integer less than this number.
Number Ceiling (Number) Returns the smallest integer value greater than this number.

XPath has rich expression functions. The above functions are basically enough. In your project, you will find that there are many query requirements based on the actual situation, you should refer to the official documents on xaph published by W3C at the end of this Article for reference. Here I will only serve as a reference. In the following sections, our application examples will not go beyond the content mentioned above. If you are interested in XPath, you should find relevant materials and books for further study after reading this article.



Back to Top

JDOM Cultivation

Programmers who have used xerces will feel that sometimes they can say something clearly in one sentence. When xerces APIs are used to implement the current situation, three or four lines of programs are required.



Back to Top

Obtain and install JDOM

In.



Back to Top

Parse XML with JDOM

All classes of the JDOM model are in org. JDOM. * In this package, org. JDOM. input. * This package contains the JDOM parser. The dombuilder function is to parse the document of the DOM model into the document of the JDOM model; the function of saxbuilder is to parse the XML tree conforming to the JDOM model from a file or stream. Since the XML sample we mentioned above is stored in a file named sample. XML, it is clear that we should use the latter as a parsing tool. The following program demonstrates the basic functions of JDOM, namely parsing an XML document and selecting some content to output to the screen.

Import java. util .*;
Import org. JDOM .*;
Import org. JDOM. Input. saxbuilder;
Public class sample1 {
Public static void main (string [] ARGs) throws exception {
Saxbuilder sb = new saxbuilder ();
Document Doc = sb. Build ("sample. xml ");
Element root = Doc. getrootelement ();
List list = root. getchildren ("disk ");
For (INT I = 0; I <list. Size (); I ++ ){
Element element = (element) list. Get (I );
String name = element. getattributevalue ("name ");
String capacity = element. getchildtext ("capacity ");
String directories = element. getchildtext ("directories ");
String files = element. getchildtext ("Files ");
System. Out. println ("disk information :");
System. Out. println ("partition drive letter:" + name );
System. Out. println ("partition capacity:" + capacity );
System. Out. println ("number of directories:" + Directories );
System. Out. println ("number of files:" + files );
System. Out. println ("-----------------------------------");
}
}
}

Program output result:

Disk information:
Partition drive letter: c
Partition capacity: 8 GB
Number of directories: 200
Number of Files: 1580
-----------------------------------
Disk information:
Partition drive letter: d
Partition capacity: 10 GB
Number of directories: 500
Number of Files: 3000
-----------------------------------

This program uses the traditional parsing method. The first-level data is collected from the root node to the subnode one by one, which is quite satisfactory. Imagine if this tree is deep enough and we want to fetch data from the third node in layer 0 (exaggerated), it would be a nightmare! The following content will ease your pain.



Back to Top

JDOM + XPath advanced

With so many benefits of JDOM and XPath, it is time for a hero to be useful.

The XPath API of JDOM is in the org. JDOM. XPath package. Look at this package, there is only one class, JDOM is so concise, everything is not so complicated. The core APIs in this class are selectnodes () and selectsinglenode (). The former returns a group of nodes based on an XPATH statement, and the latter returns the first node that meets the criteria based on an XPATH statement.

The following program uses JDOM + XPath to implement the same functions of the previous program. You can learn a lot about using XPath:

Import java. util .*;
Import org. JDOM .*;
Import org. JDOM. Input. saxbuilder;
Import org. JDOM. XPath. XPath;
Public class sample2 {
Public static void main (string [] ARGs) throws exception {
Saxbuilder sb = new saxbuilder ();
Document Doc = sb. Build ("sample. xml ");
Element root = Doc. getrootelement ();
List list = XPath. selectnodes (root, "/HD/disk ");
For (INT I = 0; I> list. Size (); I ++ ){
Element disk_element = (element) list. Get (I );
String name = disk_element.getattributevalue ("name ");
String capacity = (text) XPath. selectsinglenode (disk_element,
"// Disk [@ name = '" + name + "']/capacity/text ()"). gettextnormalize ();
String directories = (text) XPath. selectsinglenode (disk_element,
"// Disk [@ name = '" + name + "']/directories/text ()"). gettextnormalize ();
String files = (text) XPath. selectsinglenode (disk_element,
"// Disk [@ name = '" + name + "']/files/text ()"). gettextnormalize ();
System. Out. println ("disk information :");
System. Out. println ("partition drive letter:" + name );
System. Out. println ("partition capacity:" + capacity );
System. Out. println ("number of directories:" + Directories );
System. Out. println ("number of files:" + files );
System. Out. println ("-----------------------------------");
}
}
}

Output result:

Disk information:
Partition drive letter: c
Partition capacity: 8 GB
Number of directories: 200
Number of Files: 1580
-----------------------------------
Disk information:
Partition drive letter: d
Partition capacity: 10 GB
Number of directories: 500
Number of Files: 3000
-----------------------------------


Back to Top

Conclusion

Technology is evolving rapidly. Technology can be used once and for all. XML has been developing rapidly. W3C, as an authoritative Internet organization, guides the development of Internet technology. The emergence of new technologies is mostly centered around W3C standards, but there are often some alternative methods that are "too much" to generate astonishing lethality. JDOM is one of the many secrets. Just like today's popular J2EE, many open-source organizations are still quietly building their own exclusive weapons. Who can say that they will not become an epoch creation in the near future? The rise of Hibernate is shaking the cornerstone of the EJB architecture in J2EE. As long as it is a molded frame, there must be a weak weakness. As long as new technologies can tap into the weakness of the other party, they can gain a place in the industry. This article only serves as an example. I believe that after reading this fast food, readers will surely find more beautiful scenery outside the window waiting for us to travel.

References

  • W3C published authoritative documentation on XPath visit the http://www.w3.org/TR/2002/WD-DOM-Level-3-XPath-20020328

  • JDOM official website can download the latest JDOM library http://www.jdom.org

About the author

 

Xue Gu Yu is a senior Java R & D Engineer of nordsan (Beijing) Information Technology Development Co., Ltd. He is dedicated to the R & D of enterprise-level Heterogeneous Data Exchange Server products, with rich development experience in J2EE and Web Service, you can get in touch with him through rainight@126.com.

 

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.