@H_502_2@
@H_502_2@
Document对象:@H_502_2@
SAXReader reader = newSAXReader();@H_502_2@ Document document = reader.read(new File("input.xml"));@H_502_2@
2.解析XML形式的文本,得到document对象
String text = "<members></members>";@H_502_2@ Document document =DocumentHelper.parseText(text);
3.主动创建document对象.Documentdocument = DocumentHelper.createDocument();@H_502_2@ //创建根节点
Element root = document.addElement("members");
实例:
@H_502_2@
@H_502_2@
节点对象:@H_502_2@
Element root =document.getRootElement();
Elementelement=node.element(“书名");
String text=node.getText();
parentElm.remove(childElm);//从父节点删掉子节点,而不是从子节点删掉父节点@H_502_2@
节点对象属性:@H_502_2@
Attribute attribute=root.attribute("size");@H_502_2@
@H_502_2@
将文档写入XML文件:
format.setEncoding("GBK");@H_502_2@ XMLWriterwriter = newXMLWriter(newFileWriter("output.xml"),format);@H_502_2@ writer.write(document);@H_502_2@ writer.close();
@H_502_2@Dom4j在指定位置插入节点:
Element aaa =DocumentHelper.createElement("aaa");
aaa.setText("aaa");
List list =root.element("书").elements();
list.add(1,aaa);
//更新document
@H_502_2@字符串与XML的转换:@H_502_2@
Document document =DocumentHelper.parseText(text);
SAXReader reader = newSAXReader();@H_502_2@ Document document =reader.read(newFile("input.xml"));@H_502_2@ Element root=document.getRootElement();@H_502_2@ String docXmlText=document.asXML();//把文档的XML转化为字符串
String rootXmlText=root.asXML();//把根节点转化为字符串@H_502_2@ Element memberElm=root.element("member");@H_502_2@ String memberXmlText=memberElm.asXML();//把所有member子节点转化为字符串
XPath:
XPath可用来在XML文档中对元素和属性进行遍历,简化了Dom4j查找节点的过程
@H_502_2@
实例:
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>@H_502_2@ 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>@H_502_2@ 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>