dom4j xml 简单操作
package com.my.until;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
?* @author lyon.yao
?* @function:xml文件操作 需要dom4j-1.6.1.jar
?* @date:2011-10-22
?*/
public final class XmlTool {
?private static SAXReader xppReader=new SAXReader();
?/**
? * @function:根据文件路径读取xml
? * @param Path
? * @return
? * @throws FileNotFoundException
? */
?private static Document loadXml(final String path) throws DocumentException{
??Document document=xppReader.read(path);
??return document;
??
?}
?/**
? * @function:根据字符读取xml文件
? * @param chars
? * @return
? */
?private static Document loadXml(final byte[] bytes) throws DocumentException{
??ByteArrayInputStream in=new ByteArrayInputStream(bytes);
??Document document=xppReader.read(in);
??try {
???in.close();
??} catch (IOException e) {
???e.printStackTrace();
??}
??return document;
??
?}
?/**
? * @function:根据文件对象读取xml
? * @param file
? * @return
? * @throws IOException
? */
?public static Document loadXml(File file) throws DocumentException{
??Document document;
??document = xppReader.read(file);
??return document;
??
?}
?/**
? * @function:根据id查找元素下一级中的元素
? * @param element
? * @param id
? * @return
? */
?public static List<Element> findElementById(Element element,String id){
??Iterator<Element> it = element.elementIterator();
??List<Element> result=new ArrayList<Element>();
??while (it.hasNext()) {
???Element temp = (Element) it.next();
???String id_val=temp.attributeValue("id");
???if(id.equals(id_val)){
????result.add(temp);
???}
??}
??return result;
??
?}
?/**
? * @function:根据标签名查找xml
? * @param element
? * @param id
? * @return
? */
?public static List<Element> findElementByTagName(Element element,String tag_name){
??List<Element> result=new ArrayList<Element>();
??Iterator<Element> it = element.elementIterator();
??while (it.hasNext()) {
???Element temp = (Element) it.next();
???String tagName=temp.getName();
???if(tag_name.equals(tagName)){
????result.add(temp);
???}
??}
??return result;
?}
?/**
? * @function:将xml写入指定路径的文件中
? * @param document
? * @param path
? * @throws IOException
? */
?public static void writeXml(Document document,String path) throws IOException{
??FileOutputStream out_file=new FileOutputStream(path);
??XMLWriter writer=new XMLWriter(out_file);
??writer.write(document);
??writer.flush();
??writer.close();
??out_file.close();
?}
?/**
? * @function:将xml写入文件
? * @param document
? * @param file
? * @throws IOException
? */
?public static void writeXml(Document document,File file) throws IOException{
??FileOutputStream out_file=new FileOutputStream(file);
??XMLWriter writer=new XMLWriter(out_file);
??writer.write(document);
??writer.flush();
??writer.close();
??out_file.close();
?}
?/**
? * @function:将xml文件写入输出流中
? * @param document
? * @param out
? * @throws IOException
? */
?public static void writeXml(Document document,OutputStream out) throws IOException{
??XMLWriter writer=new XMLWriter(out);
??writer.write(document);
??writer.close();
?}
?/**
? * 测试入口
? * @param args
? */
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??try {
???Document doc=XmlTool.loadXml( XmlTool.class.getClassLoader().getResource("")+ "accessCogfig.xml");
???System.out.println(doc.asXML());
???String xml=doc.asXML();
???System.out.println(XmlTool.loadXml(xml.getBytes()).asXML());
???List<Element> e=XmlTool.findElementByTagName(doc.getRootElement(), "page-config");
???System.out.println(XmlTool.findElementById(e.get(0), "accessRefuse_url"));
???XmlTool.writeXml(doc, "C:\\xml.xml");
???XmlTool.writeXml(doc, new File("c:\\tmx.xml"));
???FileOutputStream out_file=new FileOutputStream(new File("c:\\tmx1.xml"));
???XmlTool.writeXml(doc, out_file);
???
??} catch (DocumentException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}catch (IOException e1) {
???// TODO Auto-generated catch block
???e1.printStackTrace();
??}
?}
}