Write a Java program and use dom4j to create an XML document named "city.xml". Pay attention to the format and data of the document

View this chapter

View job directory


Statement of needs:

Use dom4j to create an XML document named "city.xml". The format and data of the document is shown in the figure

Realization ideas:

  1. Create a Java project, add the dom4j corresponding jar package, and create the CreateCityXML class
  2. Call the createDocument method of DocumentHelper to create the root node, call the addAttribute method to set the name attribute, and set it as the root node
  3. Call the createDocument method of DocumentHelper to create a child node, and call the add method of the corresponding parent node Element object to add a child node (note the order of the parent-child relationship structure)
  4. If you need attributes, call the addAttribute setting of the Element object, if you need content, call the setText method to set the content
  5. Finally, use OutputFormat.createPrettyPrint() to create a formatted save object to save the Xml document
  6. Create the main method, call the creation method and save the method for testing

Implementation code:

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class CreateCityXML {
	private Document document;
	private Element rootElement;

	// 该方法用于创建根节点
	public void createRootElement() {
		// 创建XML文件的文档对象
		document = DocumentHelper.createDocument();
		// 创建根节点
		rootElement = DocumentHelper.createElement("country");
		// 为根节点添加属性name
		rootElement.addAttribute("name", "中国");
		// 将rootElement设为根节点
		document.setRootElement(rootElement);
	}

	// 该方法用于创建子节点
	public void createChildElement() {
		// 创建湖北省子节点
		Element hubeiElement = DocumentHelper.createElement("province");
		// 为湖北省节点添加属性
		hubeiElement.addAttribute("name", "湖北");
		// 创建武汉市节点
		Element wuhanElement = DocumentHelper.createElement("city");
		// 为武汉市节点设置文本内容
		wuhanElement.setText("武汉");
		// 创建十堰市节点
		Element shiyanElement = DocumentHelper.createElement("city");
		// 为十堰市节点设置文本内容
		shiyanElement.setText("十堰");
		// 将武汉市节点和十堰市节点添加到湖北省节点内
		hubeiElement.add(wuhanElement);
		hubeiElement.add(shiyanElement);
		// 添加湖北省子节点到根节点中
		rootElement.add(hubeiElement);

		// 创建浙江省子节点
		Element zhejiangElement = DocumentHelper.createElement("province");
		// 为浙江省节点添加属性
		zhejiangElement.addAttribute("name", "浙江");
		// 创建杭州市节点
		Element hangzhouElement = DocumentHelper.createElement("city");
		// 为杭州市节点设置文本内容
		hangzhouElement.setText("杭州");
		// 创建宁波市节点
		Element ningboElement = DocumentHelper.createElement("city");
		// 为宁波市节点设置文本内容
		ningboElement.setText("宁波");
		// 将杭州市节点和宁波市节点添加到湖北省节点内
		zhejiangElement.add(hangzhouElement);
		zhejiangElement.add(ningboElement);
		// 添加浙江省子节点到根节点中
		rootElement.add(zhejiangElement);
	}

	// 将数据写入XML文档
	public void createXML(String path) {
		// 创建OutputFormat对象,用于格式化输出
		OutputFormat format = OutputFormat.createPrettyPrint();
		// 设置文档的编码
		format.setEncoding("UTF-8");
		try {
			// 创建XMLWriter对象,用于输出XML文档
			XMLWriter writer = new XMLWriter(new FileWriter(path), format);
			// 将Document文档输入到city.xml文件中
			writer.write(document);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		CreateCityXML createCityXML = new CreateCityXML();
		// 创建文档和根节点
		createCityXML.createRootElement();
		// 创建子节点
		createCityXML.createChildElement();
		// 生成XML文件
		createCityXML.createXML("H:/city.xml");
	}
}

 

Guess you like

Origin blog.csdn.net/weixin_44893902/article/details/108959940