dom4j读写XML


package net.sourceforge.cruisecontrol.report;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

/**
 * @author Administrator
 */
public class CreateReport {
    
    static Element chartElement;
    
    public static void createReport(List<ReportVO> list, List<ReportVO> newList) {
        String projectName = list.get(0).getProjectName();
        String url = "D:/CruiseControl/webapps/cruisecontrol/fusioncharts/dataXml/" + projectName + ".xml";
        File file = new File(url);
        if (file.exists()) {
            file.delete();
        }
        try {
            
            // 使用DocumentHelper.createDocument方法建立一个文档实例
            Document document = DocumentHelper.createDocument();
            // 使用addElement方法方法创建根元素
            chartElement = document.addElement("chart");
            chartElement.addAttribute("palette", "2");
            chartElement.addAttribute("caption", "代码检查趋势图");
            chartElement.addAttribute("subCaption", projectName + "子系统");
            chartElement.addAttribute("showValues", "0");
            chartElement.addAttribute("divLineDecimalPrecision", "1");
            chartElement.addAttribute("limitsDecimalPrecision", "1");
            chartElement.addAttribute("PYAxisName", "数量");
            chartElement.addAttribute("formatNumberScale", "0");
            
            // 使用addElement方法向chart元素添加categories子元素
            Element categoriesElement = chartElement.addElement("categories");
            
            for (int i = 0; i < list.size(); i = i + 3) {
                ReportVO reportVO = list.get(i);
                Element nweElement = categoriesElement.addElement("category");
                nweElement.addAttribute("label", reportVO.getBuildTime());
            }
            
            setXml("CheckStyle", list);
            setXml("FindBugs", list);
            setXml("PMD", list);
            
            Element projectNameElement = chartElement.addElement("project");
            for (ReportVO reportVO : newList) {
                Element newFindbugsElement = projectNameElement.addElement(reportVO.getPlugName());
                newFindbugsElement.addAttribute("errorNumber", reportVO.getErrorNumber());
            }
            
            projectNameElement.addAttribute("projectName", list.get(0).getProjectName());
            
            // 输出数据到文件
            XMLWriter output;
            // 输出格式化
            OutputFormat format = OutputFormat.createPrettyPrint();
            
            format.setEncoding("GBK");
            output = new XMLWriter(new FileWriter(url), format);
            output.write(document);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        CreateXML.createXml();
        
    }
    
    public static void setXml(String plugName, List<ReportVO> list) throws DocumentException {
        Element findbugsElement = chartElement.addElement("dataset");
        findbugsElement.addAttribute("seriesName", plugName);
        findbugsElement.addAttribute("renderAs", "Line");
        for (ReportVO reportVO : list) {
            if (reportVO.getPlugName().equals(plugName)) {
                Element setElement = findbugsElement.addElement("set");
                setElement.addAttribute("value", reportVO.getErrorNumber());
            }
        }
    }
}


package net.sourceforge.cruisecontrol.report;

import java.io.File;
import java.io.FileWriter;

import org.dom4j.Document;
import org.dom4j.DocumentException;
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 CreateXML {
    
    static Element CheckStyleElement;
    
    static Element pmdElement;
    
    static Element FindBugsElement;
    
    static Element categoriesElement;
    
    static SAXReader reader = new SAXReader();
    
    public static void createXml() {
        String url = "D:/CruiseControl/webapps/cruisecontrol/fusioncharts/dataXml/data.xml";
        File file = new File(url);
        if (file.exists()) {
            file.delete();
        }
        try {
            // 使用DocumentHelper.createDocument方法建立一个文档实例
            Document document = DocumentHelper.createDocument();
            // 使用addElement方法方法创建根元素
            Element chartElement = document.addElement("chart");
            chartElement.addAttribute("caption", "各子系统代码检查结果对比图");
            chartElement.addAttribute("showLabels", "1");
            chartElement.addAttribute("showvalues", "0");
            chartElement.addAttribute("limitsDecimalPrecision", "1");
            chartElement.addAttribute("decimals", "0");
            
            // 使用addElement方法向chart元素添加categories子元素
            categoriesElement = chartElement.addElement("categories");
            
            CheckStyleElement = chartElement.addElement("dataset");
            CheckStyleElement.addAttribute("seriesName", "CheckStyle");
            CheckStyleElement.addAttribute("color", "AFD8F8");
            CheckStyleElement.addAttribute("showValues", "0");
            
            pmdElement = chartElement.addElement("dataset");
            pmdElement.addAttribute("seriesName", "PMD");
            pmdElement.addAttribute("color", "F6BD0F");
            pmdElement.addAttribute("showValues", "0");
            
            FindBugsElement = chartElement.addElement("dataset");
            FindBugsElement.addAttribute("seriesName", "FindBugs");
            FindBugsElement.addAttribute("color", "8BBA00");
            FindBugsElement.addAttribute("showValues", "0");
            
            setXml("basic");
            setXml("device");
            setXml("dispatch");
            setXml("prod");
            setXml("ss");
            setXml("tech");
            setXml("workorder");
            
            // 输出数据到文件
            XMLWriter output;
            // 输出格式化
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("GBK");
            output = new XMLWriter(new FileWriter(url), format);
            output.write(document);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
    public static void setXml(String projectName) {
        Document newDocument;
        try {
            newDocument =
                reader.read(new File("D:/CruiseControl/webapps/cruisecontrol/fusioncharts/dataXml/" + projectName
                    + ".xml"));
            
            Element rootElm = newDocument.getRootElement();
            Element memberElm = rootElm.element("project");
            Element newElm = categoriesElement.addElement("category");
            newElm.addAttribute("label", memberElm.attribute("projectName").getText());
            
            newElm = CheckStyleElement.addElement("set");
            newElm.addAttribute("value", memberElm.element("CheckStyle").attribute("errorNumber").getText());
            
            newElm = pmdElement.addElement("set");
            newElm.addAttribute("value", memberElm.element("PMD").attribute("errorNumber").getText());
            
            newElm = FindBugsElement.addElement("set");
            newElm.addAttribute("value", memberElm.element("FindBugs").attribute("errorNumber").getText());
        } catch (DocumentException e) {
            e.getMessage();
        }
    }
}


你可能感兴趣的:(dom4j)