Dom4j 是由早期开发 JDOM 的人分离出来而后独立开发的。与 JDOM 不同的是, dom4j 使用接口和抽象基类,虽然 Dom4j 的 API 相对要复杂一些,但它提供了比 JDOM 更好的灵活性。
Document对象:
SAXReader reader = newSAXReader();
Document document = reader.read(new File("input.xml"));
2.解析XML形式的文本,得到document对象
String text = "<members></members>";
Document document =DocumentHelper.parseText(text);
Documentdocument = DocumentHelper.createDocument();
//创建根节点
Element root = document.addElement("members");
实例:
节点对象:
Element root =document.getRootElement();
Elementelement=node.element(“书名");
String text=node.getText();
List nodes = rootElm.elements("member");
for (Iterator it =nodes.iterator();it.hasNext();) {
Element elm =(Element) it.next();
// dosomething
}
for(Iterator it=root.elementIterator();it.hasNext();){
Element element = (Element)it.next();
//do something
}
6.在某节点下添加子节点.
Element ageElm =newMemberElm.addElement("age");
parentElm.remove(childElm);//从父节点删掉子节点,而不是从子节点删掉父节点
Element contentElm = infoElm.addElement ("content");
contentElm.addCDATA ( diary.getContent ());
节点对象属性:
Attribute attribute=root.attribute("size");
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute =(Attribute)it.next();
String text=attribute.getText();
System.out.println(text);
}
将文档写入XML文件:
XMLWriter writer = new XMLWriter (new FileWriter ("output.xml"));
writer.write (document);
writer.close ();
format.setEncoding("GBK");
XMLWriterwriter = newXMLWriter(newFileWriter("output.xml"),format);
writer.write(document);
writer.close();
Dom4j在指定位置插入节点:
Element aaa =DocumentHelper.createElement("aaa");
aaa.setText("aaa");
List list =root.element("书").elements();
list.add(1,aaa);
//更新document
字符串与XML的转换:
String text = "<members> <member>sitinspring</member></members>";
Document document =DocumentHelper.parseText(text);
SAXReader reader = newSAXReader();
Document document =reader.read(newFile("input.xml"));
Element root=document.getRootElement();
String docXmlText=document.asXML();//把文档的XML转化为字符串
String rootXmlText=root.asXML();//把根节点转化为字符串
Element memberElm=root.element("member");
String memberXmlText=memberElm.asXML();//把所有member子节点转化为字符串
XPath:
XPath可用来在XML文档中对元素和属性进行遍历,简化了Dom4j查找节点的过程
查询节点:
实例:
XPathTest.java:
<span style="font-size:14px;">package com.example.dom4j; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class XPathTest { public static void main(String[] args) throws Exception { // 获取解析流 SAXReader reader = new SAXReader(); // 解析xml文件 Document document = reader.read("Book.xml"); // 查询id为b002的元素 Node node = document.selectSingleNode("//book[@id='b002']"); System.out.println(node); } } </span>
dom4jTest.java:
<span style="font-size:14px;">package com.example.dom4j; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.example.domain.Book; public class dom4jTest { public static void main(String[] args) throws Exception { // 需要list存放所有的book对象 List allBook = new ArrayList(); // 获取解析流 SAXReader reader = new SAXReader(); // 解析xml文件 Document document = reader.read("book.xml"); // 获取根元素 Element rootElement = document.getRootElement(); // 获取所有的书籍 List bookList = rootElement.elements(); for (int e = 0; e < bookList.size(); e++) { Book book = new Book(); // 获取每一本书 Element bookElement = (Element) bookList.get(e); String id = bookElement.attributeValue("id"); book.setId(id); // 获取每本书的所有元素:title和price List childList = bookElement.elements(); for (int i = 0; i < childList.size(); i++) { // 获得每一个子元素 Element child = (Element) childList.get(i); // 获取子元素的文本内容 String content = child.getText(); // 获取title if ("title".equals(child.getName())) { book.setTitle(content); } // 获取price if ("price".equals(child.getName())) { book.setPrice(content); } } allBook.add(book); } System.out.println(allBook); } } </span>
CURDTest.java:
<span style="font-size:14px;">package com.example.dom4j; import java.io.FileOutputStream; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class CURDTest { public static void main(String[] args) throws Exception { // 获取解析流 SAXReader reader = new SAXReader(); // 解析xml文件 Document document = reader.read("Book.xml"); //………… // 将document另存 // 创建文件保存位置 FileOutputStream fos = new FileOutputStream("Book.dom4j.xml"); XMLWriter writer = new XMLWriter(fos); writer.write(document); writer.close(); } private static void add() throws Exception { // 获取解析流 SAXReader reader = new SAXReader(); // 解析xml文件 Document document = reader.read("Book.xml"); // 添加: // 获得根元素 Element rootElement = document.getRootElement(); // 创建book元素 Element newBook = DocumentHelper.createElement("book"); // 创建属性 Attribute newBookAttribute = DocumentHelper.createAttribute(newBook,"id","b004"); // 将属性添加到newBook中 newBook.add(newBookAttribute); // 创建newBook中的元素 Element titleElement = DocumentHelper.createElement("title"); titleElement.setText("C和指针"); // 将元素添加进newBook中 newBook.add(titleElement); // 将newBook添加如root中 rootElement.add(newBook); // 将document另存 // 创建文件保存位置 FileOutputStream fos = new FileOutputStream("Book.dom4j.xml"); XMLWriter writer = new XMLWriter(fos); writer.write(document); writer.close(); } private static void delete(Document document) throws Exception { // 删除:b001 Node bookNode = document.selectSingleNode("//book[@id='b001']"); Node parent = bookNode.getParent(); Element parentElement = (Element) parent; parentElement.remove(bookNode); // 将document另存 // 创建文件保存位置 FileOutputStream fos = new FileOutputStream("Book.dom4j.xml"); XMLWriter writer = new XMLWriter(fos); writer.write(document); writer.close(); } private static void update(Document document) throws Exception { // 修改:b002 title 编程思想 Node bookNode = document.selectSingleNode("//book[@id='b002']"); Element bookElement = (Element) bookNode; Element titleElement = bookElement.element("title"); // System.out.println(titleElement.getText()); titleElement.setText("编程思想"); // 将document另存 // 创建文件保存位置 FileOutputStream fos = new FileOutputStream("Book.dom4j.xml"); XMLWriter writer = new XMLWriter(fos); writer.write(document); writer.close(); } private static void sava(Document document) throws Exception { //保存 // 将document另存 // 创建文件保存位置 FileOutputStream fos = new FileOutputStream("Book.dom4j.xml"); XMLWriter writer = new XMLWriter(fos); writer.write(document); writer.close(); } } </span>原文链接:https://www.f2er.com/xml/297430.html