java中对xml的读取和写入

java对xml操作需导入dom4j的jar包(如下):

(解析)读取xml:

package com.rj.bd.xml.jx;

import java.io.File;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * @desc	解析xml文件
 * @author	ws
 * @time	2018-10-9
 */
public class LianXi01 {
	public static void main(String[] args) throws DocumentException {
		String pathname = "E:/xml/ren.xml";
		File file = new File(pathname);
		if (file.isFile() && file.exists()) {
			System.out.println("文件存在");
			//获取文档
			SAXReader reader = new SAXReader();
			Document document = reader.read(file);
			System.out.println(document.asXML());//输出document
			//获取根元素
			Element rootE = document.getRootElement();
			System.out.println("根元素名称:"+rootE.getName());//输出根元素名字
			//获取根元素属性
			List rootA = rootE.attributes();//根元素属性存list并指定泛型为dom4j的Attribute接口
			for (Attribute eveRootA : rootA) {
				System.out.println("根元素属性:"+eveRootA.getName()+"\t"+eveRootA.getValue());//输出根元素属性的属性名和属性值
			}
			//获取根元素的子元素
			List ziE = rootE.elements();//指定泛型为Element
			for (Element eveZiE : ziE) {
				System.out.println("子元素名称:"+eveZiE.getName());
				List ziA = eveZiE.attributes();
				for (Attribute eveZiA : ziA) {
					System.out.println("子元素属性:"+eveZiA.getName()+"\t"+eveZiA.getValue());
				}
				//获取子元素中的子元素
				List ziZiE = eveZiE.elements();
				for (Element eveZiZiE : ziZiE) {
					System.out.println("子元素的子元素的名称和文本值:"+eveZiZiE.getName()+"\t"+eveZiZiE.getText());
					List ziZiA = eveZiZiE.attributes();
					for (Attribute eveZiZiA : ziZiA) {
						System.out.println("子元素的子元素的属性:"+eveZiZiA.getName()+"\t"+eveZiZiA.getValue());
					}
				}
			}
			
		}
	}
}

 

写入xml:

package com.rj.bd.xml.xr;

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

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * @desc	xml的写入	
 * @author	ws
 * @time	2018-10-9
 */
public class LianXi01 {
	public static void main(String[] args) throws IOException {
		//创建一个document对象
		Document document = DocumentHelper.createDocument();
		//加入根元素
		Element rootE = document.addElement("people");
		//加入根元素属性
		rootE.addAttribute("name", "Lily");
		rootE.addAttribute("sex", "Women");
		//加入子元素
		Element zuoYan = rootE.addElement("eyes");
		Element youYan = rootE.addElement("eyes");
		//子元素加属性和文本值
		zuoYan.addAttribute("num", "0.5");
		zuoYan.addText("左眼");
		youYan.addAttribute("num", "0.5");
		youYan.addText("右眼");
		//设定格式化输出标准
		OutputFormat format = new OutputFormat();
		format.setEncoding("UTF-8");//设置编码格式
        format.setIndent("    ");//设置首行缩进
		format.setNewlines(true);//设置换行
		//写入
		String path = "E:/xml/people.xml";
		FileWriter write = new FileWriter(path);
		XMLWriter xmlWriter = new XMLWriter(write,format);
		xmlWriter.write(document);//写入文档
		System.out.println("写入成功!");
		//关闭流对象
		xmlWriter.close();
		write.close();
	}
}

 

你可能感兴趣的:(java基础,知识点)