java解析xml字符串为实体(dom4j解析)
packagecom.smsServer.Dhst;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.DocumentHelper;
importorg.dom4j.Element;
importcom.common.object.SmsSendResponseObject;
/**
* @description 解析xml字符串
*/
publicclassTest{
publicvoidreadStringXml(Stringxml){
Documentdoc=null;
try{
// 讀取并解析XML文檔
// SAXReader就是一個(gè)管道,用一個(gè)流的方式,把xml文件讀出來
// SAXReader reader = new SAXReader(); //User.hbm.xml表示你要解析的xml文檔
// Document document = reader.read(new File("User.hbm.xml"));
// 下面的是通過解析xml字符串的
doc=DocumentHelper.parseText(xml);// 將字符串轉(zhuǎn)為XML
ElementrootElt=doc.getRootElement();// 獲取根節(jié)點(diǎn)
System.out.println("根節(jié)點(diǎn):"+rootElt.getName());// 拿到根節(jié)點(diǎn)的名稱
Iteratoriter=rootElt.elementIterator("head");// 獲取根節(jié)點(diǎn)下的子節(jié)點(diǎn)head
// 遍歷head節(jié)點(diǎn)
while(iter.hasNext()){
ElementrecordEle=(Element)iter.next();
Stringtitle=recordEle.elementTextTrim("title");// 拿到head節(jié)點(diǎn)下的子節(jié)點(diǎn)title值
System.out.println("title:"+title);
Iteratoriters=recordEle.elementIterator("script");// 獲取子節(jié)點(diǎn)head下的子節(jié)點(diǎn)script
// 遍歷Header節(jié)點(diǎn)下的Response節(jié)點(diǎn)
while(iters.hasNext()){
ElementitemEle=(Element)iters.next();
Stringusername=itemEle.elementTextTrim("username");// 拿到head下的子節(jié)點(diǎn)script下的字節(jié)點(diǎn)username的值
Stringpassword=itemEle.elementTextTrim("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
}
Iteratoriterss=rootElt.elementIterator("body");///獲取根節(jié)點(diǎn)下的子節(jié)點(diǎn)body
// 遍歷body節(jié)點(diǎn)
while(iterss.hasNext()){
ElementrecordEless=(Element)iterss.next();
Stringresult=recordEless.elementTextTrim("result");// 拿到body節(jié)點(diǎn)下的子節(jié)點(diǎn)result值
System.out.println("result:"+result);
IteratoritersElIterator=recordEless.elementIterator("form");// 獲取子節(jié)點(diǎn)body下的子節(jié)點(diǎn)form
// 遍歷Header節(jié)點(diǎn)下的Response節(jié)點(diǎn)
while(itersElIterator.hasNext()){
ElementitemEle=(Element)itersElIterator.next();
Stringbanlce=itemEle.elementTextTrim("banlce");// 拿到body下的子節(jié)點(diǎn)form下的字節(jié)點(diǎn)banlce的值
StringsubID=itemEle.elementTextTrim("subID");
System.out.println("banlce:"+banlce);
System.out.println("subID:"+subID);
}
}
}catch(DocumentException e){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
}
/**
* @description 將xml字符串轉(zhuǎn)換成map
* @param xml
* @return Map
*/
publicstaticMapreadStringXmlOut(Stringxml){
Mapmap=newHashMap();
Documentdoc=null;
try{
doc=DocumentHelper.parseText(xml);// 將字符串轉(zhuǎn)為XML
ElementrootElt=doc.getRootElement();// 獲取根節(jié)點(diǎn)
System.out.println("根節(jié)點(diǎn):"+rootElt.getName());// 拿到根節(jié)點(diǎn)的名稱
Iteratoriter=rootElt.elementIterator("head");// 獲取根節(jié)點(diǎn)下的子節(jié)點(diǎn)head
// 遍歷head節(jié)點(diǎn)
while(iter.hasNext()){
ElementrecordEle=(Element)iter.next();
Stringtitle=recordEle.elementTextTrim("title");// 拿到head節(jié)點(diǎn)下的子節(jié)點(diǎn)title值
System.out.println("title:"+title);
map.put("title",title);
Iteratoriters=recordEle.elementIterator("script");// 獲取子節(jié)點(diǎn)head下的子節(jié)點(diǎn)script
// 遍歷Header節(jié)點(diǎn)下的Response節(jié)點(diǎn)
while(iters.hasNext()){
ElementitemEle=(Element)iters.next();
Stringusername=itemEle.elementTextTrim("username");// 拿到head下的子節(jié)點(diǎn)script下的字節(jié)點(diǎn)username的值
Stringpassword=itemEle.elementTextTrim("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
map.put("username",username);
map.put("password",password);
}
}
Iteratoriterss=rootElt.elementIterator("body");///獲取根節(jié)點(diǎn)下的子節(jié)點(diǎn)body
// 遍歷body節(jié)點(diǎn)
while(iterss.hasNext()){
ElementrecordEless=(Element)iterss.next();
Stringresult=recordEless.elementTextTrim("result");// 拿到body節(jié)點(diǎn)下的子節(jié)點(diǎn)result值
System.out.println("result:"+result);
IteratoritersElIterator=recordEless.elementIterator("form");// 獲取子節(jié)點(diǎn)body下的子節(jié)點(diǎn)form
// 遍歷Header節(jié)點(diǎn)下的Response節(jié)點(diǎn)
while(itersElIterator.hasNext()){
ElementitemEle=(Element)itersElIterator.next();
Stringbanlce=itemEle.elementTextTrim("banlce");// 拿到body下的子節(jié)點(diǎn)form下的字節(jié)點(diǎn)banlce的值
StringsubID=itemEle.elementTextTrim("subID");
System.out.println("banlce:"+banlce);
System.out.println("subID:"+subID);
map.put("result",result);
map.put("banlce",banlce);
map.put("subID",subID);
}
}
}catch(DocumentException e){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
returnmap;
}
publicstaticvoidparse(Stringxml)
{
Documentdoc=null;
try{
doc=DocumentHelper.parseText(xml);// 將字符串轉(zhuǎn)為XML
ElementrootElt=doc.getRootElement();// 獲取根節(jié)點(diǎn)smsReport
Iteratoriters=rootElt.elementIterator("sendResp");// 獲取根節(jié)點(diǎn)下的子節(jié)點(diǎn)sms
while(iters.hasNext()){
ElementrecordEle1=(Element)iters.next();
Iteratoriter=recordEle1.elementIterator("sms");
inti=0;
// 遍歷sms節(jié)點(diǎn)
while(iter.hasNext()){
ElementrecordEle=(Element)iter.next();
SmsSendResponseObject r=newSmsSendResponseObject();
Stringphone=recordEle.elementTextTrim("phone");// 拿到sms節(jié)點(diǎn)下的子節(jié)點(diǎn)stat值
StringsmsID=recordEle.elementTextTrim("smsID");// 拿到sms節(jié)點(diǎn)下的子節(jié)點(diǎn)stat值
System.out.println(phone+"==="+smsID);
}
}
}catch(DocumentException e){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
// 下面是需要解析的xml字符串例子
StringxmlString=""+""+""
+""+""
+""+"0"+""
+"1000"+"36242519880716"
+""+""+"";
/*
* Test2 test = new Test2(); test.readStringXml(xmlString);
*/
Mapmap=readStringXmlOut(xmlString);
Iteratoriters=map.keySet().iterator();
while(iters.hasNext()){
Stringkey=iters.next().toString();// 拿到鍵
Stringval=map.get(key).toString();// 拿到值
System.out.println(key+"="+val);
}
Stringxml="137000000ff8080813349da9001334f0eed8c5923187000000ff8080813349da9001334f0eee045924";
parse(xml);
}
}
總結(jié)
以上是生活随笔為你收集整理的java解析xml字符串为实体(dom4j解析)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: recvfrom超时设置
- 下一篇: Erlang 入门 ---- 基础(一)