java学习---dom4j解析xml文档相关demo

1.获取解析器对象SAXReader(dom4j的sax解析器)

SAXReader reader =  new SAXReader();

2.解析xml文档

Document document = reader.read("src/book.xml");

3.如果想要完成解析,必须先获取根节点

Element root = document.getRootElement();

4.dom4j的解析必须是一层一层的解析,获取书架下面的书的节点

List books = root.elements();

5.循环遍历,取出作者的文本内容

for (Element book : books) {
			Element author = book.element("作者");
			System.out.println(author.getText());
		}

主要代码

public static void run1() throws Exception{
		//使用SAXReader解析
		SAXReader reader =  new SAXReader();
		Document document = reader.read("src/book.xml");
		Element root = document.getRootElement();
		List<Element> books = root.elements();
		for (Element book : books) {
			Element author = book.element("作者");
			System.out.println(author.getText());
		}
	}

dom4j添加子节点
主要步骤:
获取解析器对象
解析xml文档,返回Document对象
获取根节点
获取第二本书
Element book = (Element) root.elements().get(1);
//添加子节点
Element tgj = book.addElement("团购价");
//设置文本的内容
tgj.setText("9两");
//设置好看的格式
OutputFormat format = OutputFormat.createPrettyPrint();
//回写(XMLWriter类)
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book2.xml"),format);
//把document写到xml文件中
writer.write(document);
//关闭流
writer.close();

主要代码

public static void run2() throws Exception{
	//获取解析器对象
	SAXReader reader = new SAXReader();
	//解析xml文档,返回Document对象
	Document document = reader.read("src/book2.xml");
	//获取根节点
	Element root = document.getRootElement();
	//获取第二本书
	Element book = (Element) root.elements().get(1);
	//添加子节点
	Element tgj = book.addElement("团购价");
	//设置文本的内容
	tgj.setText("9两");
	//设置好看的格式
	OutputFormat format = OutputFormat.createPrettyPrint();
	//回写(XMLWriter类)
	XMLWriter writer = new XMLWriter(new FileOutputStream("src/book2.xml"),format);
	//把document写到xml文件中
	writer.write(document);
	//关闭流
	writer.close();
	}

dom4j封装类

public class MyDom4jUtil {
	public static Document getDocument(String path) throws Exception{
		SAXReader reader = new SAXReader();
		return reader.read(path);
	}
	
	public static void writeXML(Document document,String path) throws IOException, FileNotFoundException{
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new FileOutputStream(path),format);
		writer.write(document);
		writer.close();
	}
}

使用dom4j封装类修改xml的文本内容

String path = "src/book2.xml";
//解析xml文档
Document document = MyDom4jUtil.getDocument(path);
//获取根节点
Element root = document.getRootElement();
//获取第二本书
Element book2 = (Element) root.elements().get(1);
//修改团购价
book2.element("团购价").setText("1999两");
//回写
MyDom4jUtil.writeXML(document, path);

在指定位置添加子节点(demo)

String path = "src/book2.xml";
//解析xml文档
Document document = MyDom4jUtil.getDocument(path);
//获取根节点
Element root = document.getRootElement();
//获取第二本书
Element book2 = (Element) root.elements("书").get(1);
//获取第二本书下的所有节点
List<Element> list = book2.elements();
//创建元素
Element dog = DocumentHelper.createElement("狗");
//设置文本内容
dog.setText("大黄");
//操作list集合
list.add(1, dog);
//回写
MyDom4jUtil.writeXML(document, path);

你可能感兴趣的:(java,技术类)