DOM4J解析XML

        ​Dom4j是一個易用的、開源的庫,用於XML,XPath和XSLT。它應用於Java平臺,採用了Java集合框架並徹底支持DOM,SAX和JAXP。不少Java軟件讀寫XML都在使用dom4j,Sun公司的JAXM也是用dom4j。Hibernate用它來讀寫配置文件。java

        DOM4J官網下載地址:http://www.dom4j.org/dom4j-1.6.1/框架

        DOM4J解析xml文檔實例dom

package cn.iborder.xml;

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

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

public class Dom4jParseXml {
	
	//指定xml文件做爲解析對象
	public File getXmlFile() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入要解析的xml文件名:");
		String filename = scanner.nextLine();
		//System.getProperty("user.dir") 獲取項目路徑
		File file = new File(System.getProperty("user.dir")+"\\src\\"+filename);
		return file;
	}
	
	//將xml文件轉換爲Document對象
	public Document xmlToDocument(File file) {
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			document = saxReader.read(file);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			System.out.println("Xml to Document轉換失敗...");
			System.out.println(e.getMessage());
		}
		return document;
	}
	
	//解析文檔
	public void parseDocument(Document document) {
		//獲取根節點
		Element rootElement = document.getRootElement();
		System.out.println("根節點 : "+rootElement.getName());
		//遍歷輸出 
		//第一種方法
		System.out.println("第一種遍歷輸出方法");
		for (Iterator iterator = rootElement.elementIterator(); iterator.hasNext();) {
			Element childElement1 = (Element) iterator.next();
			Attribute attribute = childElement1.attribute("id");
			System.out.println("id = " + attribute.getText());
			
			for (Iterator iter = childElement1.elementIterator(); iter.hasNext();) {
				Element childElement2 = (Element) iter.next();
				System.out.println(childElement2.getName() + " = " + childElement2.getText());
			}
		}
		
		System.out.println("====================");
		//第二種方法
		System.out.println("第二種遍歷輸出方法");
		List<Element> listElement1 = rootElement.elements();
		for (Element element1 : listElement1) {
			Attribute attribute = element1.attribute("id");
			System.out.println(attribute.getName()+" = "+attribute.getText());
			List<Element> listElement2 = element1.elements();
			for (Element element2 : listElement2) {
				System.out.println(element2.getName()+" = "+element2.getText());
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Dom4jParseXml dom4jParseXml = new Dom4jParseXml();
		File file = dom4jParseXml.getXmlFile();
		Document document = dom4jParseXml.xmlToDocument(file);
		dom4jParseXml.parseDocument(document);
	}
}

        DOM4J建立xml文檔實例ui

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

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

public class Dom4JCreateXml {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//建立document對象,根節點爲class
		Element rootElement = DocumentHelper.createElement("class");
		Document document = DocumentHelper.createDocument(rootElement);
		
		//添加兩個一級子節點
		Element childElement11 = rootElement.addElement("student");
		Element childElement12 = rootElement.addElement("student");
		
		//爲一級子節點添加屬性
		childElement11.addAttribute("uid", "10086");
		childElement12.addAttribute("uid", "10010");
		
		//添加二級子節點
		childElement11.addElement("stuName").setText("李彥宏");
		childElement11.addElement("stuAge").setText("12");
		childElement12.addElement("stuName").setText("馬化騰");
		childElement12.addElement("stuAge").setText("15");
		
		//輸出生成xml文件
		OutputFormat format = new OutputFormat("    ", true);// 設置縮進爲4個空格,而且另起一行爲true
        try {	
			XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/student.xml"), format);
			xmlWriter.write(document);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}

}
相關文章
相關標籤/搜索