示例1:
package com.shengsiyuan.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");
XMLWriter xmlWriter = new XMLWriter();
xmlWriter.write(document);
OutputFormat format = new OutputFormat(" ", true);
XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student2.xml"), format);
xmlWriter2.write(document);
XMLWriter xmlWriter3 = new XMLWriter(new FileWriter("student3.xml"), format);
xmlWriter3.write(document);
xmlWriter3.close();
}
}
示例2:
package com.shengsiyuan.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("hello");
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());
}
}
示例3:
package com.shengsiyuan.dom4j;
import java.io.FileWriter;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Test3
{
public static void main(String[] args) throws Exception
{
Document document = new Document();
Element root = new Element("联系人列表").setAttribute(new Attribute("公司",
"A集团"));
document.addContent(root);
Element contactPerson = new Element("联系人");
root.addContent(contactPerson);
contactPerson
.addContent(new Element("姓名").setText("张三"))
.addContent(new Element("公司").setText("A公司"))
.addContent(new Element("电话").setText("021-55556666"))
.addContent(
new Element("地址")
.addContent(new Element("街道").setText("5街"))
.addContent(new Element("城市").setText("上海"))
.addContent(new Element("省份").setText("上海市")));
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat()
.setIndent(" ").setEncoding("gbk"));
output.output(document, new FileWriter("contact.xml"));
}
}