Dom4J的使用:
1、导入dom4j的jar包:dom4j-1.6.1.jar
2.、具体使用
1)获得解析器:
SAXReader reader=new SAXReader();
2)解析器解析文件,获得文档对象
Document doc=reader.read(new File("src/book.xml"));
3)获得要元素:
Element root=doc.getRootElement();
--Element中的方法:
getName():获得标签名。
elementText("标签名"):根据标签名获得孩子节点的内容。
attributeValue("属性名"):根据属性名获得属性值。
attributeIterator()获得该元素下的属性迭代器。
elementIterator()获得子元素迭代器。
--Attribute中的方法:
getName():获得属性名。
getValue():获得属性值。
3.写xml文件保存到本地:
1.创建文档对象:
Document doc=DocumentHelper.createDocument();
2.在文档对象中创建根元素
Element root=doc.addElement("标签名");
3.往根中增加元素
Element book=root.addElement("boot");
4.设置标签中的文本
book.setText("thinking in java");
boot.addAttribute("id","123");
6.文档对象写入文件
FileWriter filewriter=new FileWriter(new File("src/mybook.xml"));
4.设置格式:
OutputFormat xmlFormat=OutputFormat.createPrettyPrint();
xmlFormat.serEncoding("utf-8");
XMLWriter xmlWriter=new XMLWriter(fileWriter,xmlFormat);
写入文件
xmlWriter.write(doc);
关闭流
xml.writer.close();
filewriter.close();
5.xml的修改:需要读取xml文件然后根据修改构建一份新的xml写入到本地覆盖原来的。
7、例子代码
package com.test; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class TestXML { public static void main(String[] args) { TestXML testXML = new TestXML(); // testXML.readXML(); // testXML.addXML(); // testXML.deleteXML(); testXML.updateXML(); } private void readXML() { /** * XML的解析的标准步骤 * * 1:创建一个XML的解析器。 * * 2:解析器解析XML文件,返回一个Document的元素节点。 * * 3:通过Document来获取一个根节点的对象。 * * 4:通过根节点来获取底下的子节点。 */ File xmlFile = new File( "D:\\WorkSpace6.5\\java1603\\Eclipse\\Xml_Project\\src\\student.xml"); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(xmlFile); Element root = document.getRootElement(); System.out.println(root.getName() + "\t" + root.getNodeTypeName()); List<Element> childList = root.elements(); System.out.println(childList.size()); childList = root.elements("student"); System.out.println(childList.size()); for (Element stuElement : childList) { String stu_id = stuElement.attributeValue("id"); Element stu_nameElement = stuElement.element("stu_name"); String stu_name = stu_nameElement.getText(); String stu_age = stuElement.elementText("stu_age"); String stu_sex = stuElement.elementText("stu_sex"); String stu_content = stuElement.elementText("stu_content"); System.out.println(stu_id + "\t" + stu_name + "\t" + stu_age + "\t" + stu_sex + "\t" + stu_content); } } catch (Exception e) { e.printStackTrace(); } } private void addXML() { File xmlFile = new File( "D:\\WorkSpace6.5\\java1603\\Eclipse\\Xml_Project\\src\\student.xml"); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(xmlFile); Element root = document.getRootElement(); /** * 在root中添加一个student的元素节点 */ // <student></student> Element student = root.addElement("student"); // 在student元素添加属性节点 // <student id = "4"></student> student.addAttribute("id","4"); /** * 在student元素下面加入stu_name节点 */ Element stu_name = student.addElement("stu_name"); stu_name.setText("王五"); Element stu_age = student.addElement("stu_age"); stu_age.setText("30"); Element stu_sex = student.addElement("stu_sex"); stu_sex.setText("男"); Element stu_content = student.addElement("stu_content"); stu_content.addCDATA("!@#$%^&*&^YHGKPPLKjhfd;afdsl;alsdf"); /** * 将内容重新写回到XML文件中。 */ OutputStream outputStream = new FileOutputStream(xmlFile); OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setIndent(true); outputFormat.setIndent("\t"); XMLWriter xmlWriter = new XMLWriter(outputStream,outputFormat); xmlWriter.write(document); xmlWriter.flush(); xmlWriter.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 删除XML中的元素 */ private void deleteXML() { File xmlFile = new File( "D:\\WorkSpace6.5\\java1603\\Eclipse\\Xml_Project\\src\\student.xml"); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(xmlFile); Element root = document.getRootElement(); List<Element> childList = root.elements("student"); for (Element stuElement : childList) { String stu_id = stuElement.attributeValue("id"); if (stu_id.equals("5")) { root.remove(stuElement); break; } } /** * 将内容重新写回到XML文件中。 */ OutputStream outputStream = new FileOutputStream(xmlFile); OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setIndent(true); outputFormat.setIndent("\t"); XMLWriter xmlWriter = new XMLWriter(outputStream,outputFormat); xmlWriter.write(document); xmlWriter.flush(); xmlWriter.close(); } catch (Exception e) { e.printStackTrace(); } } private void updateXML() { File xmlFile = new File( "D:\\WorkSpace6.5\\java1603\\Eclipse\\Xml_Project\\src\\student.xml"); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(xmlFile); Element root = document.getRootElement(); List<Element> childList = root.elements("student"); for (Element stuElement : childList) { String stu_id = stuElement.attributeValue("id"); if (stu_id.equals("2")) { /** * 更改XML中元素节点的数据 */ Element stu_name = stuElement.element("stu_name"); Element stu_age = stuElement.element("stu_age"); Element stu_sex = stuElement.element("stu_sex"); Element stu_content = stuElement.element("stu_content"); stu_name.setText("修改操作"); stu_age.setText("20"); stu_sex.setText("gril"); /** * 由于Dom4j中没有提供CDATA节点修改的操作。 * * 变通:先删除节点,然后再添加CDATA节点的操作 */ stuElement.remove(stu_content); stu_content = stuElement.addElement("stu_content"); stu_content.addCDATA("CDATA节点被修改了.....l.asidfuaosdf"); break; } } /** * 将内容重新写回到XML文件中。 */ OutputStream outputStream = new FileOutputStream(xmlFile); OutputFormat outputFormat = OutputFormat.createPrettyPrint(); outputFormat.setIndent(true); outputFormat.setIndent("\t"); XMLWriter xmlWriter = new XMLWriter(outputStream,outputFormat); xmlWriter.write(document); xmlWriter.flush(); xmlWriter.close(); } catch (Exception e) { e.printStackTrace(); } } }8、student.xml
<?xml version="1.0" encoding="UTF-8"?> <root> <student id="1"> <stu_name>aaa</stu_name> <stu_age>21</stu_age> <stu_sex>男</stu_sex> <stu_content>1+23</stu_content> </student> <student id="2"> <stu_name>修改操作</stu_name> <stu_age>20</stu_age> <stu_sex>gril</stu_sex> <stu_content><![CDATA[CDATA节点被修改了.....l.asidfuaosdf]]></stu_content> </student> <teacher t_id="3" /> <student id="4"> <stu_name>admin</stu_name> <stu_age>10</stu_age> <stu_sex>男</stu_sex> <stu_content><![CDATA[!@#$%^&*&^YHGKPPLKjhfd;afdsl;alsdf]]></stu_content> </student> <student id="5"> <stu_name>test</stu_name> <stu_age>20</stu_age> <stu_sex>女</stu_sex> <stu_content><![CDATA[!@#$%^&*&^YHGKPPLKjhfd;afdsl;alsdf]]></stu_content> </student> <student id="6"> <stu_name>guest</stu_name> <stu_age>30</stu_age> <stu_sex>男</stu_sex> <stu_content><![CDATA[!@#$%^&*&^YHGKPPLKjhfd;afdsl;alsdf]]></stu_content> </student> </root>