通过dom4j对xml文档的读取操作工具类封装

通过dom4j对xml文档的读取操作。

下面给出两个工具类:XmlDom4jUtil.java与Configure.java

-----XmlDom4jUtil.java

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

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

public class XmlDom4jUtil {
 
 /**
  * description:从XML文档到Document对象,如果XML文档不存在,则返回的Document对象为空。
  * 解析.xml文件成一个 Document 对象
  * @param filePath eg.xml文件所在路径或文件名
  * @return
  */
 public Document createDocXMLFile(String filePath){
  Document document = null;
  File file = new File(filePath);
  if(file.exists()){
   try{
             SAXReader saxReader = new SAXReader();
             document = saxReader.read(new File(filePath));               
         }catch(Exception ex){
             ex.printStackTrace();
         }
  }
  return document;
 }
 
 /**
  * description:从Document对象到XML文档 如果Document为空或filePath为空,则返回更新失败false。
  * 将经过处理的Document 对象转化为物理存储文件xml存放到filePath路径上
  * @param document 
  * @param filePath  将要生成的xml文档存放到的路径
  * @return
  */
 public boolean updateXMLFileDoc(Document document,String filePath){
  boolean result = false;
  if(document==null||filePath==null){
   return false;
  }  
  try{          
             XMLWriter writer = null;
             /** 格式化输出,类型IE浏览一样 */
             OutputFormat format = OutputFormat.createPrettyPrint();
             /** 指定XML编码 */
             format.setEncoding("UTF-8");
             //不要用 FileWriter 输出双字节,改为 FileOutputStream 输出单字节:
             writer= new XMLWriter(new FileOutputStream(new File(filePath)),format);
             writer.write(document);
             writer.close();     
             /** 执行成功,需返回1 */
             result = true; 
         }catch(Exception ex){
             ex.printStackTrace();
         }
     return result;
 }
}

---Configure.java

import org.dom4j.Document;

import com.hymake.hyplat.app.util.AppClassUtil;
import com.hymake.hyplat.app.util.XmlDom4jUtil;

public class Configure {
 
 private String filename ="";
 
    public Configure(String fileName){
     this.filename = fileName.trim();
    }
   
    //"//approval-config.xml"
    /**
     * 获得配置文件对应的Document对象
     */
    public Document getCurrentDoc(){
  XmlDom4jUtil xmlUtil = new XmlDom4jUtil();
  String filePath = AppClassUtil.getClassRootPath(this.getClass())+"//"+filename;
  Document doc = xmlUtil.createDocXMLFile(filePath);
  return doc;
 }
   
    /**
     * 获得配置文件在应用的绝对路径
     * @return
     */
    public String getAbsoluteFilePath(){
     return AppClassUtil.getClassRootPath(this.getClass())+"//"+filename;
    }
   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nifengzhuizhao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值