Java Xml 简明教程
Java JDOM Parser - Parse XML Document
Java JDOM 解析器是一个 Java 开源 API,包含用于解析 XML 文档的类和方法。JDOM 提供对 XML 元素的随机访问,因为它使用 DOMBuilder 或 SAXBuilder 在内存中创建一个树形文档结构。在本章中,我们将看到如何使用 SAX 解析器从 XML 文件构建 JDOM 文档。
Java JDOM Parser is an open source API in Java that has classes and methods to parse XML documents. JDOM provides random access of XML elements as it creates a tree document structure inside the memory using DOMBuilder or SAXBuilder. In this chapter, we are going to see how to build a JDOM document from an XML file using a SAX Parser.
Parse XML Using JDOM Parser
以下是使用 JDOM 解析器解析文档时使用的步骤 -
Following are the steps used while parsing a document using JDOM Parser −
-
*Step 1: *Creating a SAXBuilder Object
-
*Step 2: *Reading the XML
-
*Step 3: *Parsing the XML Document
-
*Step 4: *Retrieving the Elements
Step 1: Creating a SAXBuilder Object
JDOM 文档使用 SAX 解析器如下构建 -
JDOM document is build using a SAX Parser as follows −
SAXBuilder saxBuilder = new SAXBuilder();
我们还可以使用已存在的 DOM org.w3c.dom.Document 创建 JDOM 文档,如下所示 -
We can also create JDOM document using an already existing DOM org.w3c.dom.Document as follows −
DOMBuilder domBuilder = new DOMBuilder();
Step 2: Reading the XML
XML 文件被带入文件对象,如下所示 -
An XML file is taken into a File object as follows −
File xmlFile = new File("input.xml");
我们还可以使用 StringBuilder 对象获取 XML 内容。稍后,我们可以将其转换为字节以便进行解析。
We can also take XML content using StringBuilder object. Later, we can convert it into bytes for parsing.
StringBuilder xmlBuilder = new StringBuilder();
xmlBuilder.append("<?xml version="1.0"?> <rootElement></rootElement>");
ByteArrayInputStream input = new ByteArrayInputStream( xmlBuilder.toString().getBytes("UTF-8"));
Step 3: Parsing the XML Document
使用 build() 函数,我们解析 XML 文件或输入流。它从给定的文件或输入流构建 JDOM 文档。当解析文档时出现错误时,它将引发 JDOMException 和 IOException。
Using build() function, we parse an XML file or input stream. It builds the JDOM document from the given file or input stream. It throws JDOMException and IOException when there are errors in parsing the document.
Document document = saxBuilder.build(input);
Step 4: Retrieving the Elements
在遵循前三个步骤之后,我们已成功从我们的 XML 文件或流中构建了 JDOM 文档。我们现在可以使用 Document 和 Element 类中可用方法从文档中获取所有相关信息。
After following the first three steps, we have successfully build JDOM document from our XML file or stream. We can now use methods available in Document and Element classes to obtain all the related information from the document.
Retrieving Root Element
Document 接口的 getRootElement() 方法以 Element 对象的形式返回文档的根元素。
The method getRootElement() of Document interface returns the root element of the document in the form of an Element object.
Element 对象上的 getName() 方法以字符串的形式返回元素的名称。
The getName() method on Element object returns the name of the element in the form of a String.
Example
以下 RetrieveRootElement.java 程序将 XML 内容放入 StringBuilder 对象中。然后将其转换为字节,并使用 build() 函数进行解析。它检索根元素并打印根元素的名称。
The following RetrieveRootElement.java program takes XML content in a StringBuilder object. It is then converted into bytes and parsed using build() function. It retrieves the root element and prints the name of the root element.
import java.io.ByteArrayInputStream;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class RetrieveRootElement {
public static void main(String args[]) {
try {
//Creating a SAXBuilder Object
SAXBuilder saxBuilder = new SAXBuilder();
//Reading the XML
StringBuilder xmlBuilder = new StringBuilder();
xmlBuilder.append("<class></class>");
ByteArrayInputStream input = new ByteArrayInputStream(xmlBuilder.toString().getBytes("UTF-8"));
//Parsing the XML Document
Document document = saxBuilder.build(input);
//Retrieving the Root Element Name
Element root_element = document.getRootElement();
System.out.println("Root Element Name : " + root_element.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Retrieving Child Elements
要检索元素的子元素,在 Element 对象上会使用 getChildren() 方法。它会以列表的形式返回子元素。此列表包含 Element 对象中的所有子元素。
To retrieve child elements of an element, getChildren() method is used on the Element object. It returns the child elements in the form of a list. This list contains all the child elements in the of Element objects.
要检索元素的文本内容,在 Element 对象上会使用 getText() 方法。它会返回 Element 的起始标签和结束标签之间的内容。
To retrieve text content of an element, getText() method is used on the Element object. It returns the content between the opening and closing tags of an Element.
Example
我们向 class 元素添加三个 student 子元素,并将该文件保存为 student.xml 。每个 student 元素的文本内容中都提到了学生姓名。
Let us add three student child elements to our class element and save this file as student.xml. The name of the student is mentioned in the text content of each student element.
<?xml version = "1.0"?>
<class>
<student>dinkar</student>
<student>Vaneet</student>
<student>jasvir</student>
</class>
现在,下面的 Java 程序会读取 student.xml 文件,并检索所有子元素及其文本内容。
Now, the following java program reads the student.xml file and retrieves all the child elements along with their text content.
import java.io.File;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class RetrievingChildElements {
public static void main(String[] args) {
try {
//Creating a SAXBuilder Object
SAXBuilder saxBuilder = new SAXBuilder();
//Reading the XML
File inputFile = new File("student.xml");
//Parsing the XML Document
Document document = saxBuilder.build(inputFile);
//Retrieving Root Element
Element RootElement = document.getRootElement();
System.out.println("Root element :" + RootElement.getName());
//Retrieving Child Elements
List<Element> studentList = RootElement.getChildren();
System.out.println("----------------------------");
for (int temp = 0; temp < studentList.size(); temp++) {
Element student = studentList.get(temp);
System.out.println("\nCurrent Element :" + student.getName());
System.out.println("Text Content :" + student.getText());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Retrieving Attributes
Element 对象上的 getAttribute("attr_name") 方法会将属性名称作为参数,并以 Attribute 对象的形式检索该属性。如果元素中不存在此属性,则它会返回 null。
The getAttribute("attr_name") method on an Element object takes attribute name as an argument and retrieves the attribute in the form of Attribute object. If there is no such attribute in an element, it returns null.
Attribute 对象上的 getValue() 方法会检索属性值作为文本内容。
The getValue() method on an Attribute object retrieves the value of the attribute as textual content.
Example
要 student.xml 文件,让我们向 student 元素添加一些子元素以及属性“rollno”。现在,我们使用 JDOM 解析器 API 尝试检索所有这些信息。
To student.xml file, let us add some child elements to student element along with the attribute, "rollno". Now, let us try to retrieve all this information using JDOM parser API.
<?xml version = "1.0"?>
<class>
<student rollno = "393">
<firstname>dinkar</firstname>
<lastname>kad</lastname>
<nickname>dinkar</nickname>
<marks>85</marks>
</student>
<student rollno = "493">
<firstname>Vaneet</firstname>
<lastname>Gupta</lastname>
<nickname>vinni</nickname>
<marks>95</marks>
</student>
<student rollno = "593">
<firstname>jasvir</firstname>
<lastname>singn</lastname>
<nickname>jazz</nickname>
<marks>90</marks>
</student>
</class>
在以下 RetrievingAttributes.java 程序中,我们首先在 Element 列表中收集所有子元素,然后使用 getChild() 方法获取 student 元素中每个子元素的详细信息。
In the following RetrievingAttributes.java program, we have first collected all the child elements in an Element list and then used getChild() method to get the details of each child inside the student element.
import java.io.File;
import java.util.List;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class RetrievingAttributes {
public static void main(String[] args) {
try {
//Creating a SAXBuilder Object
SAXBuilder saxBuilder = new SAXBuilder();
//Reading the XML
File inputFile = new File("student.xml");
//Parsing the XML Document
Document document = saxBuilder.build(inputFile);
//Retrieving Root Element
Element RootElement = document.getRootElement();
System.out.println("Root element :" + RootElement.getName());
//Retrieving Child Elements and Attributes
List<Element> studentList = RootElement.getChildren();
System.out.println("----------------------------");
for (int temp = 0; temp < studentList.size(); temp++) {
Element student = studentList.get(temp);
System.out.println("\nCurrent Element :"
+ student.getName());
Attribute attribute = student.getAttribute("rollno");
System.out.println("Student roll no : "
+ attribute.getValue() );
System.out.println("First Name : "
+ student.getChild("firstname").getText());
System.out.println("Last Name : "
+ student.getChild("lastname").getText());
System.out.println("Nick Name : "
+ student.getChild("nickname").getText());
System.out.println("Marks : "
+ student.getChild("marks").getText());
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Output
每个学生的信息都会与其学号一起显示。
Information of each student is dispalyed along with their roll numbers.
Root element :class
----------------------------
Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 85
Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95
Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singn
Nick Name : jazz
Marks : 90