DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准,是以层次结构组织的节点或信息片断的集合,它总是把文档看成一个文档树,SAX处理能够立即开始,而不是等待所有的数据被处理,对于大型文档来说是个巨大的优点,JDOM无疑转为Java平台设计,它简化与XML的交互并且比使用DOM更快,但是它大量的试用具体类操作从而降低了操作的灵活性,dom4j代表了全新的开发结果,集成的XPath支持、XML Schema支持以及用于大文档或流化文档的基于事件的处理,它大量的使用了接口定义,提供了比JDOM大得多的灵活性。下面是一个简单的生成xml例子:
package
org.bulktree.xml;
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
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
*/
public class XmlTest02 {
public Document createDocument() throws DocumentException {
/**//*
* 产生 一个document对象
*/
Document doc = DocumentHelper.createDocument();
/**//*
* 添加一个跟元素
*/
Element root = doc.addElement("Students");
Element child1 = root.addElement("student");
/**//*
* 设置属性ID
*/
child1.addAttribute("ID", "001");
Element subChild1 = child1.addElement("name");
subChild1.setText("bulktree");
Element subChild2 = child1.addElement("age");
subChild2.setText("22");
Element subChild3 = child1.addElement("sex");
subChild3.setText("男");
Element child2 = root.addElement("student");
child2.addAttribute("ID", "002");
child2.addElement("name").setText("laoshulin");
child2.addElement("age").setText("22");
child2.addElement("sex").setText("女");
return doc;
}
public void writerDocument() throws Exception {
/**//*
* 格式化输出
*/
OutputFormat of = new OutputFormat(" ", true);
/**//*
* 输出到屏幕
*/
// XMLWriter xmlWriter = new XMLWriter(System.out, of);
/**//*
* 输出到流中
*/
XMLWriter xmlWriter = new XMLWriter(new FileWriter("student.xml"), of);
xmlWriter.write(this.createDocument());
xmlWriter.close();
}
public static void main(String[] args) throws Exception {
new XmlTest02().writerDocument();
}
}
import java.io.FileWriter;
import java.io.UnsupportedEncodingException;
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
*/
public class XmlTest02 {
public Document createDocument() throws DocumentException {
/**//*
* 产生 一个document对象
*/
Document doc = DocumentHelper.createDocument();
/**//*
* 添加一个跟元素
*/
Element root = doc.addElement("Students");
Element child1 = root.addElement("student");
/**//*
* 设置属性ID
*/
child1.addAttribute("ID", "001");
Element subChild1 = child1.addElement("name");
subChild1.setText("bulktree");
Element subChild2 = child1.addElement("age");
subChild2.setText("22");
Element subChild3 = child1.addElement("sex");
subChild3.setText("男");
Element child2 = root.addElement("student");
child2.addAttribute("ID", "002");
child2.addElement("name").setText("laoshulin");
child2.addElement("age").setText("22");
child2.addElement("sex").setText("女");
return doc;
}
public void writerDocument() throws Exception {
/**//*
* 格式化输出
*/
OutputFormat of = new OutputFormat(" ", true);
/**//*
* 输出到屏幕
*/
// XMLWriter xmlWriter = new XMLWriter(System.out, of);
/**//*
* 输出到流中
*/
XMLWriter xmlWriter = new XMLWriter(new FileWriter("student.xml"), of);
xmlWriter.write(this.createDocument());
xmlWriter.close();
}
public static void main(String[] args) throws Exception {
new XmlTest02().writerDocument();
}
}