使用DOM4J构建XML:
package com.rui.dom4j;
import java.io.FileOutputStream;
import java.io.FileWriter;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Test1 {
public static void main(String[] args) throws Exception {
/*
// 创建文档的根节点
Document document = DocumentHelper.createDocument();
// 创建元素
Element root = DocumentHelper.createElement("student");
// 设置根元素节点
document.setRootElement(root);
*/
Element root = DocumentHelper.createElement("student");
Document document = DocumentHelper.createDocument(root);
root.addAttribute("name", "zhangsan");
Element helloElement = root.addElement("hello");
Element worldElement = root.addElement("world");
helloElement.setText("hello");
worldElement.setText("world");
helloElement.addAttribute("age", "20");
worldElement.addAttribute("age", "21");
// 输出到 控制台
XMLWriter xmlWriter = new XMLWriter();//(System.out);
xmlWriter.write(document);
OutputFormat format = new OutputFormat(" ",true,"UTF-8");
// 使用字节流
XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"),format);
xmlWriter2.write(document);
// 使用字符流
XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"),format);
xmlWriter3.write(document);
// 注意字符流方式要使用 flush()方法清空缓冲区 和 使用 close() 方法去关闭字符流
xmlWriter3.flush();
xmlWriter3.close();
Runtime.getRuntime().exit(0);
}
}
使用DOM4J解析XML:
package com.rui.dom4j;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.DOMReader;
import org.dom4j.io.SAXReader;
public class Test2 {
public static void main(String[] args) throws Exception {
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File("student2.xml"));
Element root = doc.getRootElement();
System.out.println("root element: " + root.getName());
List childList = root.elements();
System.out.println(childList.size());
List childList2 = root.elements("hello");
System.out.println(childList2.size());
Element first = root.element("world");
System.out.println(first.getName());
System.out.println(first.attributeValue("age"));
for(Iterator iter = root.elementIterator(); iter.hasNext();){
Element e = (Element)iter.next();
System.out.println(e.attributeValue("age"));
}
System.out.println("----------------------------------");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document document = db.parse(new File("student2.xml"));
DOMReader domReader = new DOMReader();
// 将 JAXP 的 Document 转换为 dom4j的Document
Document d = domReader.read(document);
Element rootElement = d.getRootElement();
System.out.println(rootElement.getName());
System.exit(0);
}
}
使Dom4j在断网情况下不下载DTD文件进行XML校验,设置dom4j属性如下:
import com.sun.org.apache.xerces.internal.impl.Constants;
SAXReader saxReader = new SAXReader();
saxReader.setValidation(false);
// saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
saxReader.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE, false);
Document mybatisConfig = saxReader.read(SpringContextHolder.class.getClassLoader().getResourceAsStream("mapper/Configuration.xml"));
项目如附件: