日期:2014-05-17  浏览次数:20825 次

dom4j解析xml问题
Java code

package com.huateng.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.util.List;


import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ExtraUtil {
    Document doc = null;
    Element rootElement = null;
    String label = null;
    String editor = null;
    boolean createLabel = false;
    boolean createEditor = false;
    public ExtraUtil(Document doc,String label,String editor){
        this.doc = doc;
        this.rootElement = doc.getRootElement();
        this.label = label;
        this.editor = editor;
        this.init();
    }
    public void init() { 
        if(this.label!=null&&!this.label.equals("")){
            this.createLabel = true;
        }
        if(this.editor!=null&&!this.editor.equals("")){
            this.createEditor = true;
        }
        //如果一个label标签都没有,添加FieldLabels
        if(this.createLabel){
            if(this.rootElement.selectNodes("FieldLabels").size()==0){
                this.rootElement.addElement("FieldLabels");
            }
        }
        //如果一个editor标签都没有,添加Editors
        if(this.createEditor){
            if(this.rootElement.selectNodes("Editors").size()==0){
                this.rootElement.addElement("Editors");
            }
        }
} 
    public static Document read(String fileName) throws MalformedURLException, DocumentException { 
           SAXReader reader = new SAXReader(); 
           Document document = reader.read(new File(fileName));
           System.out.println(document.getStringValue());
           return document; 
        } 
    public static void main(String[] args) {
        //对应xml的位置
        String docPath = "D:\\work\\SICS\\WEB-INF\\ExtraUtilTest.xml";
        //对应的dataset名字
        String datasetName = "GlobalData";
        //要添加的label的后缀,如果不需要生成label=""即可
        String label = "Label";
        //要添加的editor的后缀
        String editor = "";
        
        try{
            Document doc = ExtraUtil.read(docPath);
            ExtraUtil extraUtil = new ExtraUtil(doc,label,editor);
            //Element rootElement = doc.getRootElement();
            
            Element datasetElement = extraUtil.getDataSetByName(datasetName);
            if(datasetElement==null) throw new Exception("不存在的dataset");
            
            extraUtil.createLabel(datasetElement);
            
            //去掉换行
            
            //转换为window格式
            
            //写入原文件
            XMLWriter output;
            OutputFormat format = OutputFormat.createCompactFormat();
            format.setEncoding("UTF-8");
            //输出文件时定义已UTF-8形成文档
            FileWriter fileWriter = new FileWriter(docPath);
            OutputStreamWriter out = new OutputStreamWriter(
                    new FileOutputStream(docPath),"UTF-8");
            //out.write(doc.getStringValue());
            //out.close();
            output = new XMLWriter(fileWriter, format);
            output.write(doc);
            output.close();

        }catch(Exception e){
            e.printStackTrace();
        }

    }
    
    public Element getDataSetByName(String datasetName) { 
            List datasets = ((Element)this.rootElement.selectNodes("Datasets").get(0)).selectNodes("Dataset");
            for(int i=0;i<datasets.size();i++){
                Element dataset = (Element)datasets.get(