Java Xml 简明教程
Java JDOM Parser - Query XML Document
Java JDOM 解析器是一个 API,包含类和方法,用于从 XML 文件构建 JDOM 文档以查询相关信息。在本章中,我们将使用 getText() 方法按文本内容查询元素,并使用 getAttributeValue() 方法按属性查询元素。
Java JDOM parser is an API which has classes and methods to build JDOM documents from XML files to query related information. In this chapter, we are going to query elements by text content using getText() method and to query elements by attributes, we are using getAttributeValue() method.
Query XML Using JDOM Parser
以下是我们需要按照的步骤,使用 JDOM 解析器查询 XML 文档 −
Following are the steps we need to follow to query an XML document using JDOM parser −
-
*Step 1: *Creating a SAXBuilder Object
-
*Step 2: *Reading the XML
-
*Step 3: *Parsing the XML Document
-
*Step 4: *Querying the Elements
就前三个步骤,请参阅 this chapter
Refer this chapter for first three steps
Step 4: Querying the Elements
完成前三个步骤后,我们将获得一个 JDOM 文档。使用 org.jdom2 包中提供的类和方法,我们可以开始查询元素及其属性。
After completing the first three steps, we get a JDOM document. Using classes and methods present in org.jdom2 package, we can start querying the elements and their attributes.
现在,我们将看到两个示例,说明如何根据元素的文本内容及其属性查询元素。我们对这两个示例都使用相同的 cars.xml 文件。
Now, we are going to see two examples on how to query elements based on their text content and their attributes. We use the same cars.xml file for both the examples.
Querying Elements by TextContent
我们可以通过先使用 getRootElement() 方法获取根元素,按元素的文本内容查询元素。获取根元素后,我们可以使用 getChildren() 函数获取所有子元素。然后,我们可以使用 getText() 方法按文本内容查询元素。
We can query elements by their text content, by first getting the root element using getRootElement() method. After we obtain the root element, we can use getChildren() function to get all the child elements. Then, we can query the elements by text content using getText() method.
Example
考虑以下 cars.xml 文件,其中 carname 元素带有 company 属性和文本内容。现在,我们将查询此 XML 文件以查找“Bentley 2”汽车。
Consider the following cars.xml file with carname elements having company attribute and text content. Now, we are going to query this XML file to find "Bentley 2" car.
<?xml version = "1.0"?>
<cars>
<carname company="Ferarri" >Ferarri 101</carname>
<carname company="Lamborgini">Lamborgini 001</carname>
<carname company="Lamborgini">Lamborgini 002</carname>
<carname company="Lamborgini">Lamborgini 003</carname>
<carname company="Bentley">Bentley 1</carname>
<carname company="Bentley">Bentley 2</carname>
<carname company="Bentley">Bentley 3</carname>
</cars>
在以下 QueryXMLElements.java 程序中,我们使用 SAXBuilder 解析 cars.xml 文件以查询所有 carname 元素。在 Element 列表中获取 carname 元素后,我们将迭代列表以查找“Bentley 2”汽车。
In the following QueryXMLElements.java program, we are parsing the cars.xml file using SAXBuilder to query all the carname elements. After getting the carname elements in an Element list, we are iterating the list to find "Bentley 2" car.
import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import java.util.List;
public class QueryXMLElements {
public static void main(String args[]) {
try {
//Creating a SAXBuilder Object
SAXBuilder saxBuilder = new SAXBuilder();
//Reading the XML
File inputFile = new File("cars.xml");
//Parsing the XML Document
Document document = saxBuilder.build(inputFile);
//Retrieving the Root Element
Element RootElement = document.getRootElement();
List<Element> carList = RootElement.getChildren("carname");
//Finding "Bentley 2" car in the list
boolean found=false;
for(int index=0; index<carList.size();index++) {
Element car = carList.get(index);
if(car.getText().equals("Bentley 2")) {
found=true;
break;
}
}
if(found) {
System.out.println("Bentley 2 car is found");
}
else {
System.out.println("Bentley 2 car is not found");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
输出窗口显示在 XML 文件中找到了“Bentley 2”汽车。
The output window displays that the "Bentley 2" car is found in the XML file.
Bentley 2 car is found
Querying Elements by Attributes
元素可以具有文本内容以及属性。现在,我们使用相同的 cars.xml 文件按 company 属性查询 carname 元素。
Elements can also have attributes along with the text content. Now, let use the same cars.xml file to query the carname elements by their company attribute.
Element 类的 getAttributeValue("attr_name") 方法以字符串参数的形式获取属性名称,并返回属性的相应值。
The getAttributeValue("attr_name") method of Element class takes attribute name as a String argument and returns the corresponding value of the attribute.
Example
在以下 QueryAttributes.java 程序中,我们从 getChildren() 方法中获取 carname Elements 列表。然后,我们迭代该列表,并在 company 属性值等于“Bentley”时增加计数。
In the following QueryAttributes.java program, we are getting the list of carname Elements from getChildren() method. Then, we are iterating the list and incrementing the count when the company attribute value is equal to "Bentley".
import java.io.File;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import java.util.List;
public class QueryAttributes {
public static void main(String args[]) {
try {
//Creating a SAXBuilder Object
SAXBuilder saxBuilder = new SAXBuilder();
//Reading the XML
File inputFile = new File("cars.xml");
//Parsing the XML Document
Document document = saxBuilder.build(inputFile);
//Retrieving the Root Element
Element RootElement = document.getRootElement();
List<Element> carList = RootElement.getChildren("carname");
//Counting Bentley cars
int count=0;
for(int index=0; index<carList.size();index++) {
Element car = carList.get(index);
if(car.getAttributeValue("company").equals("Bentley")) {
count++;
}
}
System.out.println("Total number of Bentley cars : " + count);
} catch(Exception e) {
e.printStackTrace();
}
}
}