DOM4J解析XML
SAXReader reader = new SAXReader();
Document document = reader.read(new File("input.xml"));
2.解析XML形式的文本,得到document对象.
String text = "<members></members>";
Document document = DocumentHelper.parseText(text);
3.主动创建document对象.
Document document = DocumentHelper.createDocument();
//创建根节点
Element root = document.addElement("members");
节点对象:
Element root = document.getRootElement();
Element element=node.element(“书名");
String text=node.getText();
List nodes = rootElm.elements("member");
for (Iterator it = nodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
// do something
}
Element element = (Element) it.next();
// do something
}
6.在某节点下添加子节点.
Element ageElm = newMemberElm.addElement("age");
element.setText("29");
//childElm是待删除的节点,parentElm是其父节点
parentElm.remove(childElm);
Element contentElm = infoElm.addElement("content");
contentElm.addCDATA(diary.getContent());
Element root=document.getRootElement();
//属性名name
Attribute attribute=root.attribute("size");
2.取得属性的文字
Attribute attribute=root.attribute("size");
root.remove(attribute);
Element root=document.getRootElement();
for(Iterator it=root.attributeIterator();it.hasNext();){
Attribute attribute = (Attribute) it.next();
String text=attribute.getText();
System.out.println(text);
}
5.设置某节点的属性和文字.
6.设置属性的文字
attribute.setText("sitinspring");
XMLWriter writer = new XMLWriter(new FileWriter("output.xml"));
writer.write(document);
writer.close();
2.文档中含有中文,设置编码格式写入的形式.
// 指定XML编码
format.setEncoding("GBK");
XMLWriter
writer = new XMLWriter(newFileWriter("output.xml"),format);
writer.write(document);
writer.close();
字符串与XML的转换 :
String text = "<members> <member>sitinspring</member></members>";
Document document = DocumentHelper.parseText(text);
2.将文档或节点的XML转化为字符串.
SAXReader reader = new SAXReader();
Document document = reader.read(new
File("input.xml"));
Element root=document.getRootElement();
String docXmlText=document.asXML();
String rootXmlText=root.asXML();
Element memberElm=root.element("member");
String memberXmlText=memberElm.asXML();
首先需要导入这两个jar包:
第一个是必须的,第二个是为后边使用XPath使用的。
对XML文档的CRUD操作:
package com.dom4j.cn; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.List; import org.dom4j.Attribute; 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.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class Dom4JDemo { /* * 读取指定标签的内容,以及他的属性 */ @Test public void read1() throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); Element book = (Element) root.elements("书").get(0); String value = book.element("书名").getText(); System.out.println("标签内容:" + value); Attribute attr = book.element("书名").attribute("name"); System.out.println("标签属性 : " + attr.getName() + " 属性内容 : " + attr.getText()); } /* * 在标签末尾添加标签 */ @Test public void add1() throws DocumentException, IOException { SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); Element book = root.element("书"); book.addElement("售价").setText("100005415"); //利用Format解决乱码问题。 注意XMLWriter的第一个参数要使用字节流 //如果它使用字符流的话,那么一定要指定该字符流的码表与format的一致 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format); writer.write(document); writer.close(); /* * 字符流的形式 */ /*OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8"), format); writer.write(document); writer.close();*/ } /* * 在指定标签的后边添加标签 */ @Test public void add2() throws DocumentException, IOException { SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); Element book = root.element("书"); List list = book.elements(); Element price = DocumentHelper.createElement("加价"); price.setText("500"); //设置新标签的内容 price.addAttribute("ok", "yes"); // 设置新标签的属性 list.add(2, price); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format); writer.write(document); } /* * 删除指定标签 或者 属性 */ @Test public void delete() throws DocumentException, IOException { SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); //删除标签 /*Element price = root.element("书").element("售价"); price.getParent().remove(price);*/ Element bookName = root.element("书").element("书名"); bookName.remove(bookName.attribute("name")); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format); writer.write(document); } /* * 更新指定标签内容或者属性 */ @Test public void update() throws DocumentException, IOException { SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); //更新标签内同 // root.element("书").element("书名").setText("欧亚"); //更新标签属性 root.element("书").element("书名").attribute("name").setText("qqqq"); OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"), format); writer.write(document); } }
XMLPath类似于正则有非常强大的功能来取标签, 这里简单模拟一个登陆判断用户名密码的。 可以查看API
package com.dom4j.cn; import java.io.File; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class XMLPathDemo { public static void main(String[] args) throws DocumentException { String username = "aa"; String password = "111"; SAXReader reader = new SAXReader(); Document document = reader.read(new File("src/users.xml")); Node node = document.selectSingleNode("//user[@username='"+username+"' and @password='"+password+"']"); if (node == null) { System.out.println("用户名密码错误"); } else { System.out.println("登陆成功"); } } }
XPathAPI :http://pan.baidu.com/s/1i3zDpNv
DOM4J jar包: http://pan.baidu.com/s/1sj6O5R3 http://pan.baidu.com/s/1c0Fw81a