---
title: "dom4j  xml 简单操作"
author: "yaoweinan"
date: "2011-10-24T11:02:14Z"
tags: []
categories: ["编程语言"]
slug: "dom4j-xml"
draft: false
lastmod: "2012-06-04T23:23:24Z"
id: 1209468
url: "https://ilyon.iteye.com/blog/1209468"
---

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&lt;Element&gt; findElementById(Element element,String id){  
  Iterator&lt;Element&gt; it = element.elementIterator();  
  List&lt;Element&gt; result=new ArrayList&lt;Element&gt;();  
  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&lt;Element&gt; findElementByTagName(Element element,String tag\_name){  
  List&lt;Element&gt; result=new ArrayList&lt;Element&gt;();  
  Iterator&lt;Element&gt; 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&lt;Element&gt; 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();  
  }

 }

}