Java Xml 简明教程
Java DOM4J Parser - Create XML Document
Java DOM4J 库具有创建 XML 文档的类、接口和方法。org.dom4j 包的 Document 和 Element 接口具有创建 XML 文档的方法。org.dom4j.io 包的 XMLWriter 类将文档的 XML 内容写入文件。在本章中,我们将使用这些接口和类创建 XML 文件。
Java DOM4J library has classes, interfaces and methods to create XML documents. The interfaces Document and Element of org.dom4j package has methods to create XML documents. The XMLWriter class of org.dom4j.io package writes the XML content of the document into the file. In this chapter we are going to use these interfaces and classes to create XML files.
Create XML Using Java DOM4J Parser
以下是使用 Java DOM4J 解析器创建 XML 文档的步骤 −
Following are the steps to create XML documents using Java DOM4J Parser −
-
*Step 1: *Creating a new Document
-
*Step 2: *Creating and Adding XML elements
-
*Step 3: *Creating a FileOutputStream
-
*Step 4: *Writing the XML document into file
-
*Step 5: *Printing the XML document
Step 1: Creating a new Document
org.dom4j 包中的 DocumentHelper 类提供了使用 DOM4J 的帮助器方法。createDocument() 方法如下创建新文档:
The DocumentHelper class of org.dom4j package provides the helper methods to use DOM4J. The createDocument() method creates a new document as follows −
Document document = DocumentHelper.createDocument();
Step 2: Creating and Adding XML elements
我们可以使用 addElement() 方法创建根元素并将其添加到 XML 文档。此外,我们可以使用同一个 addElement() 方法将子元素添加到根元素。要添加属性,我们使用 addAttribute() 方法。
We can create root element and add it to the XML document using addElement() method. Also, we can add child elements to the root element using the same addElement() method. To add attributes, we use addAttribute() method.
Element root = document.addElement( "root_ele_name" );
Element child = root.addElement("child_ele_name").addAttribute("attr_name", "attr_val");
Step 3: Creating a FileOutputStream
要将 XML 文档的内容复制到文件中,我们需要如下创建一个 FileOutputStream:
To copy the content of XML document into a file, we need to create a FileOutputStream as follows −
FileOutputStream newFile = new FileOutputStream("D://newDOM4jFile.xml");
Step 4: Writing the XML document into file
XMLWriter 类具有 write() 方法,用于将 XML 文档写入指定文件。我们还可以将我们想要添加到内容中的格式作为第二个参数发送给 write() 方法。
The XMLWriter class has write() method to write the XML document into the specified file. We can also send the format we want to add to the content as second argument to the write() method.
XMLWriter writer = new XMLWriter(newFile);
writer.write( document );
Step 5: Printing the XML document
要打印 XML 文档的内容,我们可以将 System.out 作为第一个参数传递,并将其格式作为第二个参数传递。createPrettyPrint() 方法在显示时创建正确的 XML 元素缩进。此步骤是可选的,可用于测试目的。
To print the content of XML document, we can pass System.out as the first argument and format as the second argument. The createPrettyPrint() method creates proper indentation of XML elements while displaying. This step is optional and can be used for testing purpose.
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter consoleWriter = new XMLWriter( System.out, format );
consoleWriter.write( document );
Creating XML file
addElement() 方法可与 Document 或 Element 接口配合使用。此方法将元素的名称作为参数,并将其添加到指定的文档或元素中。要添加根元素,我们使用 Document.addElement(root_ele_name)。
The addElement() method can be used with either Document or Element interface. This method takes name of the element as a parameter and adds it to the specified document or element. To add root element, we use Document.addElement(root_ele_name).
addAttribute() 方法将属性名称作为第一个参数,将值作为第二个参数。它将属性添加到指定的元素。
The addAttribute() method takes name of the attribute as first argument and value as the second argument. It adds the attribute to the specified element.
addText() 方法向元素添加文本内容。它将文本内容作为 String,并添加到相应的元素。
The addText() method adds the text content to the element. It takes the text content as a String and adds to the corresponding element.
Example
使用上面讨论的方法,我们将创建一个 XML 文件,其根元素为“cars”,公司为其属性。让我们添加两个“carname”元素,其中“type”作为属性。
Using the methods discussed above, we are going to create an XML file with 'cars' as root element and company as its attribute. Let us add two 'carname' elements with 'type' as attribute.
我们需要创建一个以下 cars.xml 文件:
We need to create the following cars.xml file −
<?xml version = "1.0" encoding = "UTF-8"?>
<cars>
<supercars company = "Ferrari">
<carname type = "formula one">Ferrari 101</carname>
<carname type = "sports">Ferrari 202</carname>
</supercars>
</cars>
以下 CreateXMLDemo.java 程序创建了一个文档,并使用 addElement() 方法添加元素,使用 addAttribute() 方法添加属性。要设置文本内容,请使用 addText() 方法。文档的 XML 内容被传输到一个文件,并打印在控制台上。
The following CreateXMLDemo.java program creates a document and adds the elements using addElement() method and attributes using addAttribute() method. To set the text content, addText() method is used. The XML content of the document is transferred into a file and also printed on the console.
package java_xml_dom4j;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class CreateDemo {
public static void main(String[] args) {
try {
//Creating a new Document
Document document = DocumentHelper.createDocument();
//Creating and Adding XML elements
Element root = document.addElement( "cars" );
Element supercarElement = root.addElement("supercars")
.addAttribute("company", "Ferrai");
supercarElement.addElement("carname")
.addAttribute("type", "Ferrari 101")
.addText("Ferrari 101");
supercarElement.addElement("carname")
.addAttribute("type", "sports")
.addText("Ferrari 202");
//Creating a FileOutputStream
FileOutputStream newFile = new FileOutputStream("D://newDOM4jFile.xml");
//Writing the XML document into file
XMLWriter writer = new XMLWriter(newFile);
writer.write( document );
//Printing the XML document
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter consoleWriter = new XMLWriter( System.out, format );
consoleWriter.write( document );
} catch (Exception e) {
e.printStackTrace();
}
}
}