maven中使用dom4j解析、生成XML的简易方法
此片文章主要写一些关于如何在maven工程中使用dom4j来解析或生成XML的建议方法,实际可使用的写法不仅限于如下所写的样例代码。此处进攻快速入手和提供思路使用。
首先配置pom.xml中的依赖的包
- <dependencies>
- <dependency>
- <groupId>dom4j</groupId>
- <artifactId>dom4j</artifactId>
- <version>1.6.1</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- </dependency>
- <dependency>
- <groupId>jaxen</groupId>
- <artifactId>jaxen</artifactId>
- <version>1.1.6</version>
- </dependency>
- </dependencies>
上述配置中jaxen的包主要在使用Xpath时会被用到。
需要解析的样例XML文件
- <?xml version="1.0" encoding="UTF-8" ?>
- <resources>
- <product name="QQ">
- <account id="123456789">
- <nickname>qq-account-1</nickname>
- <password>qwe123qwe123</password>
- <level>56</level>
- </account>
- <account id="987654321">
- <nickname>qq-account-2</nickname>
- <password>ios9ios9ios9</password>
- <level>12</level>
- </account>
- </product>
- <product name="Netease">
- <account id="tom">
- <password>pwdOfTom</password>
- <capacity>1024</capacity>
- </account>
- <account name="Jim">
- <password>pwdOfJim</password>
- <capacity>2560</capacity>
- </account>
- </product>
- </resources>
解析的java类
- package com.jeremy;
- import org.dom4j.*;
- import org.dom4j.io.SAXReader;
- import org.dom4j.io.XMLWriter;
- import org.dom4j.xpath.DefaultXPath;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- /**
- * Created by jeremysong on 1/7/2016.
- */
- public class DomParser
- {
- public static List<String> parseMethod1() throws DocumentException
- {
- List<String> result = new ArrayList<String>();
- SAXReader reader = new SAXReader();
- Document document = reader.read("E:\\IdeaProjects\\Example\\NetApp\\src\\main\\resources\\target.xml");
- XPath xPath = new DefaultXPath("/resources/product[@name='QQ']/account[@id='987654321']/password");
- List<Element> list = xPath.selectNodes(document.getRootElement());
- for (Element element : list)
- {
- System.out.println(element.getTextTrim());
- result.add(element.getTextTrim());
- }
- return result;
- }
- public static List<String> parseMethod2() throws DocumentException
- {
- List<String> result = new ArrayList<String>();
- SAXReader reader = new SAXReader();
- Document document = reader.read("E:\\IdeaProjects\\Example\\NetApp\\src\\main\\resources\\target.xml");
- List<Element> products = document.getRootElement().selectNodes("/resources/product");
- Iterator iterator = products.iterator();
- while (iterator.hasNext())
- {
- Element product = (Element) iterator.next();
- String name = product.attributeValue("name");
- System.out.println(name);
- }
- return result;
- }
- public static void generate()
- {
- Document doc = DocumentHelper.createDocument();
- Element root = doc.addElement("resources");
- Element product = root.addElement("product");
- product.addAttribute("name", "QQ");
- Element account = product.addElement("account");
- account.addAttribute("id", "123456789");
- Element nickname = account.addElement("nickname");
- nickname.setText("QQ-account-1");
- Element password = account.addElement("password");
- password.setText("123asd21qda");
- Element level = account.addElement("level");
- level.setText("56");
- PrintWriter pWriter = null;
- XMLWriter xWriter = null;
- try
- {
- pWriter = new PrintWriter("E:\\IdeaProjects\\Example\\NetApp\\src\\main\\generate.xml");
- xWriter = new XMLWriter(pWriter);
- xWriter.write(doc);
- } catch (Exception e)
- {
- e.printStackTrace();
- } finally
- {
- try
- {
- xWriter.flush();
- xWriter.close();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
测试类:
- import com.jeremy.DomParser;
- import org.dom4j.DocumentException;
- import org.junit.Test;
- /**
- * Created by jeremysong on 1/7/2016.
- */
- public class TestDomParser
- {
- @Test
- public void testParseMethod1() throws DocumentException
- {
- DomParser.parseMethod1();
- }
- @Test
- public void testParseMethod2() throws DocumentException
- {
- DomParser.parseMethod2();
- }
- @Test
- public void testGenerate()
- {
- DomParser.generate();
- }
- }
测试结果:
testParseMethod1运行结果
testParseMethod2运行结果
testGenerate运行结果
- <?xml version="1.0" encoding="UTF-8"?>
- <resources>
- <product name="QQ">
- <account id="123456789">
- <nickname>QQ-account-1</nickname>
- <password>123asd21qda</password>
- <level>56</level>
- </account>
- </product>
- </resources>
引用原文:https://blog.csdn.net/jeremysong88/article/details/50479924
写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!
如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!
给自己的梦想添加一双翅膀,让它可以在天空中自由自在的飞翔!