2019独角兽企业重金招聘Python工程师标准>>>
各大数据接口大全:http://blog.sina.com.cn/s/articlelist_2127818045_10_1.html
package com.test.junit;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import com.test.bean.res.TextMessage;
import com.test.util.MessageUtil;
public class test {
@Test
public void test() throws DocumentException {
String request="<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgfsdType></xml>";
InputStream in_nocode = new ByteArrayInputStream(request.getBytes());
Map<String, String> map = new HashMap<String, String>();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(in_nocode);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
assertEquals(4,map.keySet().size());
for(String e:map.keySet()){
System.out.print( map.get(e)+"\n");
}
}
// @Test
// public void test2() throws DocumentException{
// TextMessage tx =new TextMessage();
// tx.setContent("sd");
// tx.setCreateTime(23232323);
// tx.setFromUserName("dd");
// tx.setMsgType("msgType");
// tx.setToUserName("dsd");
// tx.setFuncFlag(34);
// String xml=MessageUtil.textMessageToXml(tx);
// System.out.print(xml);
// }
}