当前位置:   article > 正文

Dom4j解析xml文件_org.dom4j.element解析列表

org.dom4j.element解析列表

Dom4j是dom4j.org出品的一个开源XML解析包。Dom4j是一个易用的、开源的库,用于XML,XPath和XSLT。它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX和JAXP。Hibernate也是用它来读写配置文件的。

一、DOM4j的下载  
dom4j-1.6.1.jar 下载:http://sourceforge.net/directory/os:windows/freshness:recently-updated/?q=dom4j  


二、利用dom4j创建一个内容如下的xml文件SqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
  3. "ibatis-3-config.dtd">
  4. <configuration>
  5. <environments default="development">
  6. <environment id="MySQL">
  7. <transactionManager type="JDBC">123</transactionManager>
  8. <dataSource type="POOLED">
  9. <property name="driver" value="com.mysql.jdbc.Driver"/>
  10. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
  11. <property name="username" value="root"/>
  12. <property name="password" value="admin"/>
  13. </dataSource>
  14. </environment>
  15. </environments>
  16. <mappers>
  17. <mapper resource="com/pojo/sql/DepMap.xml"/>
  18. <mapper resource="com/pojo/sql/EmpMap.xml"/>
  19. </mappers>
  20. </configuration>


步骤:
 1.创建一个输出流,指定创建的SqlMapConfig.xml文件的位置-------OutputStream
    OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
 2.创建一个xml编写器-------XMLWriter
    XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
    这里os是前面新建的一个输出流,OutputFormat.createPrettyPrint()是指定输出的格式的,这里代表内容缩进。
 3.创建xml文档-------Document、DocumentHelper
    Document document = DocumentHelper.createDocument();
 4.创建里面的所有结点元素-------Elemenet
    Element configuration = document.addElement("configuration");
   ..............
    新建结点元素的形式是:Element 子结点 = 父节点.addElement("子结点名");
    给结点元素添加属性是:结点.addAttribute("属性名", "属性值");
 5.保存xml
    writer.write(document);
代码如下:

  1. public class CreateXML {
  2. /**
  3. * @param args
  4. * @throws IOException
  5. */
  6. public static void main(String[] args) throws IOException {
  7. //创建一个输出流,指定创建的xml文件的位置
  8. OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
  9. XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
  10. //创建Document
  11. Document document = DocumentHelper.createDocument();
  12. Element configuration = document.addElement("configuration");
  13. Element environments = configuration.addElement("environments");
  14. environments.addAttribute("default", "development");
  15. Element environment = environments.addElement("environment");
  16. environment.addAttribute("id", "MySQL");
  17. Element transactionManager = environment.addElement("transactionManager");
  18. transactionManager.addAttribute("type", "JDBC");
  19. transactionManager.setText("123");
  20. Element dataSource = environment.addElement("dataSource");
  21. dataSource.addAttribute("type", "POOLED");
  22. Element driver = dataSource.addElement("property");
  23. driver.addAttribute("name", "driver");
  24. driver.addAttribute("value", "com.mysql.jdbc.Driver");
  25. Element url = dataSource.addElement("property");
  26. url.addAttribute("name", "url");
  27. url.addAttribute("value", "jdbc:mysql://127.0.0.1:3306/test");
  28. Element username = dataSource.addElement("property");
  29. username.addAttribute("name", "username");
  30. username.addAttribute("value", "root");
  31. Element password = dataSource.addElement("property");
  32. password.addAttribute("name", "password");
  33. password.addAttribute("value", "admin");
  34. Element mappers = configuration.addElement("mappers");
  35. Element mapper1 = mappers.addElement("mapper");
  36. mapper1.addAttribute("resource", "com/pojo/sql/DepMap.xml");
  37. Element mapper2 = mappers.addElement("mapper");
  38. mapper2.addAttribute("resource", "com/pojo/sql/EmpMap.xml");
  39. //保存xml
  40. writer.write(document);
  41. writer.close();
  42. }
  43. }

二、查找xml文件的内容
    查找xml文件内容有两种情况,第一种就是查找出所有同名的结点元素然后遍历、第二种就是直接找到某个结点元素
    比如遍历SqlMapConfig.xml,将driver、url、username、password的值读取出来就有两种方法,代码如下
方法一:

  1. public class GetNodes {
  2. /**方法一:
  3. * 遍历新建的xml,将driver、url、username、password的值读取出来
  4. * @param args
  5. * @throws IOException
  6. * @throws DocumentException
  7. */
  8. public static void main(String[] args) throws IOException, DocumentException {
  9. SAXReader reader = new SAXReader();
  10. Document document = reader.read("d:/SqlMapConfig.xml");
  11. List<Element> elements = document.selectNodes("/configuration/environments/environment/dataSource/property");
  12. String driver = "";
  13. String url = "";
  14. String username = "";
  15. String password = "";
  16. for (Element e : elements) {
  17. String name = e.attributeValue("name");
  18. String value = e.attributeValue("value");
  19. if("driver".equals(name)){
  20. driver = value;
  21. }else if("url".equals(name)){
  22. url = value;
  23. }else if("username".equals(name)){
  24. username = value;
  25. }else if("password".equals(name)){
  26. password = value;
  27. }
  28. }
  29. System.out.println("driver:"+driver+",url:"+url+",username:"+username+",password:"+password);
  30. }
  31. }

这里List<Element> elements = document.selectNodes("/configuration/environments/environment/dataSource/property")是指将名为property的结点元素全部找出来,用一个List接收。
方法二:

  1. public class GetNodes1 {
  2. /**方法二:
  3. * 将driver、url、username、password的值读取出来
  4. * @param args
  5. * @throws IOException
  6. * @throws DocumentException
  7. */
  8. public static void main(String[] args) throws IOException, DocumentException {
  9. SAXReader reader = new SAXReader();
  10. Document document = reader.read("d:/SqlMapConfig.xml");
  11. Element driverElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='driver']");
  12. Element urlElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='url']");
  13. Element usernameElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='username']");
  14. Element passwordElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']");
  15. String driver = driverElement.attributeValue("value");
  16. String url = urlElement.attributeValue("value");
  17. String username = usernameElement.attributeValue("value");
  18. String password = passwordElement.attributeValue("value");
  19. System.out.println("driver:"+driver+",url:"+url+",username:"+username+",password:"+password);
  20. }
  21. }

这种方法就没有遍历了,就是直接将要查找的结点找出来,用selectSingleNode()函数。这里的[@name='driver']是指名找到哪一个property结点,相当于一个键值对,表示找有name="driver"属性的property结点。
形式为[@属性名='属性值']。


三、修改XML文件
    步骤:
    1.读取要修改的xml文件
    2.查找到要修改的结点
    3.保存
代码如下:

  1. public class UpdateNode {
  2. /**
  3. * 修改密码为123456
  4. * @param args
  5. * @throws DocumentException
  6. * @throws IOException
  7. */
  8. public static void main(String[] args) throws DocumentException, IOException {
  9. //读取并修改
  10. SAXReader reader = new SAXReader();
  11. Document document = reader.read("d:/SqlMapConfig.xml");
  12. Element passwordElement = (Element)document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']");
  13. passwordElement.addAttribute("value", "123456");
  14. //保存
  15. OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
  16. XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
  17. writer.write(document);
  18. writer.close();
  19. os.close();
  20. }
  21. }

四、删除XML文件的某个结点元素
  删除和修改一样,也是要先找到要删除的结点,然后删除之,最后保存修改过的文件。删除结点的形式为:父节点.remove(子节点);
代码如下:

  1. public class DeleteNode {
  2. /**
  3. * 删除密码结点
  4. * @param args
  5. * @throws DocumentException
  6. * @throws IOException
  7. */
  8. public static void main(String[] args) throws DocumentException, IOException {
  9. //找到结点并删除
  10. SAXReader reader = new SAXReader();
  11. Document document = reader.read("d:/SqlMapConfig.xml");
  12. Element passwordElement = (Element) document.selectSingleNode("/configuration/environments/environment/dataSource/property[@name='password']");
  13. passwordElement.getParent().remove(passwordElement);
  14. //保存
  15. OutputStream os = new FileOutputStream("d:/SqlMapConfig.xml");
  16. XMLWriter writer = new XMLWriter(os,OutputFormat.createPrettyPrint());
  17. writer.write(document);
  18. writer.close();
  19. os.close();
  20. }
  21. }


声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读