解析XML—— Dom4j的SAX解析

<?xml version="1.0" encoding="UTF-8"?>

<书架> 
  <书> 
    <书名>葵花宝典</书名>  
    <作者>岳不群</作者>  
    <售价>998两</售价>  
    <简介>欲练此功...</简介> 
  </书>  
  <书> 
    <书名>降龙十八掌</书名>  
    <猫>我是猫</猫>  
    <作者 age="101">欧阳峰</作者>  
    <售价>9两</售价>  
    <简介>这书不错啊</简介>  
    <猫>我是猫</猫> 
  </书> 
</书架>

 

package com.ygang.topic02;

import java.io.IOException;
import java.util.List;

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

import com.ygang.utils.Dom4jParseUtil;

public class Dom4j_DomParse {

	public static void main(String[] args) {
		// 1、读
		read();

		// 2、写(插入)
		add();

		// 3、删
		delete();

		// 4、改
		update();

	}

	/**
	 * 改:
	 * 将第二本书作者改为 欧阳峰 ,添加属性 age="101"
	 */
	private static void update() {
		String xmlPath = "src/book2.xml";
		Document document;
		try {
			document = new SAXReader().read(xmlPath);
			Element root = document.getRootElement();
			Element secondBook = (Element) root.elements("书").get(1);
			Element author = secondBook.element("作者");
			author.addAttribute("age","101").setText("欧阳峰");

			// 回写
			Dom4jParseUtil.write(document, xmlPath);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void delete() {
		String xmlPath = "src/book2.xml";
		Document document;
		try {
			document = new SAXReader().read(xmlPath);
			Element root = document.getRootElement();
			Element firstBook = (Element) root.elements("书").get(0);
			firstBook.remove(firstBook.element("狗"));

			// 回写
			Dom4jParseUtil.write(document, xmlPath);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加(插入): 在第一本书“简介”前加入<狗>有粮有米的Tom</狗>
	 */
	private static void add() {
		String xmlPath = "src/book2.xml";
		Document document;
		try {
			document = new SAXReader().read(xmlPath);
			Element root = document.getRootElement();
			Element firstBook = (Element) root.elements("书").get(0);

			Element dog = DocumentHelper.createElement("狗");
			dog.addText("有粮有米的Tom");

			List list = firstBook.elements();
			list.add(3, dog);

			// 回写
			Dom4jParseUtil.write(document, xmlPath);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * DOM4J的SAX解析,读取文件,获取两本书的售价
	 * 
	 * @throws DocumentException
	 */
	public static void read() {
		String xmlPath = "src/book2.xml";
		Document document;
		try {
			document = new SAXReader().read(xmlPath);
			Element root = document.getRootElement();
			List<Element> books = root.elements("书");
			for (Element book : books) {
				Element price = book.element("售价");
				System.out.println(price.getText());
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
}
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public abstract class Dom4jParseUtil {
	private Dom4jParseUtil(){}
	
	// 漂亮格式回写
	public static void write(Document document,String xmlPath) throws IOException {
		OutputFormat format = OutputFormat.createPrettyPrint();
		XMLWriter writer = new XMLWriter(new FileWriter(xmlPath), format);
		writer.write(document);
		writer.close();
	}
}

 

 

你可能感兴趣的:(java,dom4j,sax,xml解析,SAXReader,outputformat,documenthelper,XMLWriter)