maven中使用dom4j解析、生成XML的简易方法

此片文章主要写一些关于如何在maven工程中使用dom4j来解析或生成XML的建议方法,实际可使用的写法不仅限于如下所写的样例代码。此处进攻快速入手和提供思路使用。


首先配置pom.xml中的依赖的包

    
        
            dom4j
            dom4j
            1.6.1
        
        
            junit
            junit
            4.11
        
        
            jaxen
            jaxen
            1.1.6
        
    

上述配置中jaxen的包主要在使用Xpath时会被用到。


需要解析的样例XML文件



    
        
            qq-account-1
            qwe123qwe123
            56
        
        
            qq-account-2
            ios9ios9ios9
            12
        
    
    
        
            pwdOfTom
            1024
        
        
            pwdOfJim
            2560
        
    

解析的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 parseMethod1() throws DocumentException
    {
        List result = new ArrayList();

        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 list = xPath.selectNodes(document.getRootElement());
        for (Element element : list)
        {
            System.out.println(element.getTextTrim());
            result.add(element.getTextTrim());
        }
        return result;
    }

    public static List parseMethod2() throws DocumentException
    {
        List result = new ArrayList();

        SAXReader reader = new SAXReader();
        Document document = reader.read("E:\\IdeaProjects\\Example\\NetApp\\src\\main\\resources\\target.xml");
        List 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运行结果

maven中使用dom4j解析、生成XML的简易方法_第1张图片

testParseMethod2运行结果

maven中使用dom4j解析、生成XML的简易方法_第2张图片


testGenerate运行结果



    
        
            QQ-account-1
            123asd21qda
            56
        
    


你可能感兴趣的:(java,项目管理)