第一个是原创的,在读取模板xml文件,然后添加内容
第二个是转载的,直接创建xml文件,引用http://blog.csdn.net/hzc543806053/article/details/7310418/
package TestPMD;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
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 addxml {
public static void main(String[] args) throws DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("e:\\cppdemo.xml"));
Element root = document.getRootElement();
Element elementRule=creatXml("12","12","12","//WhileStatement[not(Statement/Block)]","class Example {void bar() {while (baz)buz.doSomething();}");
root.add(elementRule);
Element elementRule2=creatXml("12","12","12","//ClassOrInterfaceDeclaration[not (matches(@Image,'^([A-Z][a-z|0-9]+)+$'))]","class Example {void bar() {while (baz)buz.doSomething();}");
root.add(elementRule2);
try {
writerDocumentToNewFile(document);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Element creatXml(String name,String message,String description,String value,String example){
Element elementRule = DocumentHelper.createElement("rule");
elementRule.addAttribute("name", name);
elementRule.addAttribute("language", "java");
elementRule.addAttribute("message", message);
elementRule.addAttribute("class", "net.sourceforge.pmd.lang.rule.XPathRule");
elementRule.addElement("description").addText(description);
Element elementPros=elementRule.addElement("properties");
Element elementPro=elementPros.addElement("property").addAttribute("name", "xpath");
elementPro.addElement("value").addCDATA(value);
elementRule.addElement("priority").addText("3");
elementRule.addElement("example").addCDATA(example);
return elementRule;
}
public static void writerDocumentToNewFile(Document document)throws Exception{
//输出格式
OutputFormat format = OutputFormat.createPrettyPrint();
//设置编码
format.setEncoding("UTF-8");
//XMLWriter 指定输出文件以及格式
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("e:\\demo1.xml")),"UTF-8"), format);
//写入新文件
writer.write(document);
writer.flush();
writer.close();
}
}
package dds;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
* 用DOM4J创建xml文档
*
*
*/
public class test {
public static void main(String[] args) throws Exception {
//1.第一种 创建文档及设置根元素节点的方式
//创建文档的根节点
// Document document = DocumentHelper.createDocument();
// //创建文档的 根元素节点
// Element root = DocumentHelper.createElement("Person");
// document.setRootElement(root);
//2.第二种 创建文档及设置根元素节点的方式
Element root = DocumentHelper.createElement("Person");
Document document = DocumentHelper.createDocument(root);
//给根节点添加属性
root.addAttribute("学校", "南大").addAttribute("位置", "江西");
//给根节点添加孩子节点
Element element1 = root.addElement("学生");
element1.addElement("姓名").addAttribute("婚姻", "单身").addText("小章");
element1.addElement("年龄").addText("21");
Element element2 = root.addElement("学生");
element2.addElement("姓名").addAttribute("婚姻", "单身").addText("小红").addElement("爱好").addText("唱歌");
element2.addElement("年龄").addText("22");
//把生成的xml文档存放在硬盘上 true代表是否换行
OutputFormat format = new OutputFormat(" ",true);
format.setEncoding("GBK");//设置编码格式
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("Person.xml"),format);
xmlWriter.write(document);
xmlWriter.close();
}
}