Maven+Dom4j

dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件的。dom4j是一个十分优秀的JavaXML API,具有性能优异、功能强大和极其易使用的特点,它的性能超过sun公司官方的dom技术,同时它也是一个开放源代码的软件,可以在SourceForge上找到它。在IBM developerWorks上面还可以找到一篇文章,对主流的Java XML API进行的性能、功能和易用性的评测,所以可以知道dom4j无论在哪个方面都是非常出色的。如今可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这已经是必须使用的jar包, Hibernate也用它来读写配置文件。


pom.xml添加依赖

  
      dom4j
      dom4j
      1.6
    
    
      junit
      junit
      4.12
    


Dom4j实例:

package dom4j;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import org.dom4j.*;


import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;





public class Dom4j {

    @Test
    public void createXML() {
        Document doc = DocumentHelper.createDocument();
        Element root = doc.addElement("root");
        Element father = root.addElement("father");
        Element child = father.addElement("child");
        Element name = child.addElement("name");
        name.setText("十六笔画");
        Element sex = child.addElement("sex");
        sex.setText("男");
        try {
            PrintWriter pw = new PrintWriter("e:\\wc.xml");
            OutputFormat format = OutputFormat.createPrettyPrint(); // 创建文件输出的时候,自动缩进的格式
            format.setEncoding("UTF-8");//设置编码
            XMLWriter xw = new XMLWriter(pw, format);
            xw.write(doc);
            xw.flush();
            xw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("Over");
        }
    }

    public void parseXML() {
        String rootPath = getClass().getClassLoader().getResource("").toString();
        rootPath = rootPath.substring(rootPath.indexOf("/"));
        File myXML = new File(rootPath + "queryUserReq.xml");
        SAXReader sr = new SAXReader();
        try {
            Document doc = sr.read(myXML);
            System.out.println(doc.asXML());    //Document 转成String

            Element root = doc.getRootElement();
            Map values = new HashMap();

            getAllElements(root,values);        //将Xml文件的全部节点获取Text转成Map格式
            Set keys=values.keySet();
            for (String string:keys) {
                System.out.println(string+":"+values.get(string));
            }

            Element e=   root.element("CONTROL");  //获取Element下的某一节点
            Element el=   e.element("REQUEST_ID");
            System.out.println(el.getText());


            for (Iterator fathers = root.elementIterator(); fathers.hasNext(); ) {  //遍历输出Xml节点元素
                Element father = (Element) fathers.next();
                System.out.println("    "+father.getName()+"---"+father.getText().trim());
                for (Iterator childs = father.elementIterator(); childs.hasNext(); ) {
                    Element child = (Element) childs.next();
                    System.out.println("        "+child.getName()+"--"+child.getText().trim());
                    for (Iterator nodes = child.elementIterator(); nodes.hasNext(); ) {
                        Element node = (Element) nodes.next();
                        System.out.println("            "+node.getName() + "-----" + node.getText().trim());
                    }
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


    public void getAllElements(Element element, Map values) {

        Iterator childs = element.elementIterator();
        while (childs.hasNext()) {
            Element child = (Element) childs.next();
            values.put(child.getName(), child.getTextTrim());
            getAllElements(child, values);
        }
    }

    public static void main(String[] args) {
        new Dom4j().parseXML();
    }
}




Xml文件:


	
		
		${requestId}           			
		   		
		                   
		            	   
		  
						
		              
		${requestTime}    
	
		
		
			${userName}          
			${accountType}	  
		
	




你可能感兴趣的:(webservice,maven,dom4j)