A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
|JDOM/XPath programming guide
Xue Gu Yu (email@example.com), Senior Java engineer, nordsan Information Technology Development Co., Ltd.
May 01, 2004
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.
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 ):
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:
"*" Indicates "all. /HD/* indicates all nodes in HD.
The following XPath returns all nodes named "disk" under any node:
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:
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.
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.
Obtain and install JDOM
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.
Program output result:
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.
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:
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.
About the author
Start building with 50+ products and up to 12 months usage for Elastic Compute Service