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();
  }

 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值