dom4j编辑xml报文并解析相同标签的中的属性值测试代码

package dom4jTest;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

public class Dom4jtest {

@Test
public void dom4jtest() throws Exception{
	dom4jxml();
}






private Object dom4jxml() throws Exception {
	
	   Document document = DocumentHelper.createDocument();	
	   Element Service = document.addElement("Service");  
	   Element ServiceHander = Service.addElement("ServiceHander"); 
	   Element ServiceBody = Service.addElement("ServiceBody"); 
	   Element Response = ServiceBody.addElement("Response"); 
	   Element group = Response.addElement("group"); 
	   Element complex1 = group.addElement("complex"); 
	   Element complex2 = group.addElement("complex"); 
	   Element complex3 = group.addElement("complex"); 
	   
	   group.addAttribute("name","Ratnghistory");
	   
	   Element servicesn = ServiceHander.addElement("element"); 
	   servicesn.addAttribute("name", "service_sn");
	   servicesn.addAttribute("value", "1040");
	   Element servicesn2 = ServiceHander.addElement("element"); 
	   servicesn2.addAttribute("name", "service_sn2");
	   servicesn2.addAttribute("value", "1040");
	   Element servicesn3 = ServiceHander.addElement("element"); 
	   servicesn3.addAttribute("name", "service_sn3");
	   servicesn3.addAttribute("value", "1040");
	   Element servicesn4 = ServiceHander.addElement("element"); 
	   servicesn4.addAttribute("name", "service_sn4");
	   servicesn4.addAttribute("value", "1040");
	   
	   Element complex11 = complex1.addElement("element"); 
	   complex11.addAttribute("name", "complex1");
	   complex11.addAttribute("value", "complex1");
	   Element complex111 = complex1.addElement("element"); 
	   complex111.addAttribute("name", "complex2");
	   complex111.addAttribute("value", "complex1");
	
	   Element complex22 = complex2.addElement("element"); 
	   complex22.addAttribute("name", "complex22");
	   complex22.addAttribute("value", "complex22");
	   Element complex222 = complex2.addElement("element"); 
	   complex222.addAttribute("name", "complex3");
	   complex222.addAttribute("value", "complex22");
	   Element complex33 = complex3.addElement("element"); 
	   complex33.addAttribute("name", "complex4");
	   complex33.addAttribute("value", "complex33");
	   Element complex333 = complex3.addElement("element"); 
	   complex333.addAttribute("name", "complex5");
	   complex333.addAttribute("value", "complex33");
	   OutputFormat format = OutputFormat.createPrettyPrint();//设置编码格式
	   format.setEncoding( "UTF-8");
	   String s=document. asXML();
	   List> personList = new ArrayList>();
	   try {
	        Document document3 = DocumentHelper.parseText(s);
	        Element rootElement = document3.getRootElement();// 获取根节点Service
	        Element Bodys=rootElement.element("ServiceBody");//获取根节点下的子节点Body 
			Element responsess=Bodys.element("Response");//获取body下的response
			Element grop=responsess.element("group");//获取response下的 grope
	       for (Iterator iterator = grop.elementIterator(); iterator.hasNext();) {
	           Element element = (Element) iterator.next();
	           Map personMap = new HashMap();


	    
	           for (Iterator iterator2 = element.elementIterator(); iterator2.hasNext();) {
	               Element element2 = (Element) iterator2.next();
	               personMap.put(element2.attributeValue("name"), element2.attributeValue("value"));// 将子节点的名称和值分别插入map中
	           }
	           personList.add(personMap);//将map插入list中
	       }
	   } catch (DocumentException e) {
	       e.printStackTrace();
	   }
	   System.out.println(personList);
	 
	return "";
}

}

你可能感兴趣的:(dom4j编辑xml报文并解析相同标签的中的属性值测试代码)