dom4j构建 解析XML数据

 

---------------------------------构建XML-------------------------------

PrintWriter out = resp.getWriter();

Element response = new Element("response");

Document doc = new Document(response);

Element resultCode = new Element("resultCode");

resultCode.addContent("0");

Element desc = new Element("desc");

desc.addContent("结果正确返回啦 !!!!! ");

Element num = new Element("num");

num.addContent("58");

Element commentinfolist=new Element("commentinfolist");

 

       for(int i = 0;i<=2;i++){

Element commentinfo = new Element("commentinfo");

Element commentid = new Element("commentid");

Element objectid = new Element("objectid");

Element objecttype = new Element("objecttype");

Element commentcontentcopy = new Element("commentcontentcopy");

Element createddate = new Element("createddate");

Element status = new Element("status");

Element thumbsup = new Element("thumbsup");

Element thumbsdown = new Element("thumbsdown");

Element ipaddress = new Element("ipaddress");

Element objectName = new Element("objectName");

commentid.addContent(i+"");

objectid.addContent(i+i+"");

objecttype.addContent(i+"");

commentcontentcopy.addContent(i+"commentcontentcopy");

createddate.addContent(i+"");

status.addContent("2");

thumbsup.addContent( "20");

thumbsdown.addContent( "20");

ipaddress.addContent( "192.168.0.20");

objectName.addContent("wang");

commentinfo.addContent(commentid) ;

commentinfo.addContent(objectid) ;

commentinfo.addContent(objecttype) ;

commentinfo.addContent(commentcontentcopy) ;

commentinfo.addContent(createddate) ;

commentinfo.addContent(status) ;

commentinfo.addContent(thumbsup) ;

commentinfo.addContent(thumbsdown) ;

commentinfo.addContent(ipaddress) ;

commentinfo.addContent(objectName) ;

commentinfolist.addContent(commentinfo);

}

response.addContent(resultCode);

response.addContent(desc);

response.addContent(num);

response.addContent(commentinfolist);

XMLOutputter xmlOutput = new XMLOutputter();

xmlOutput.output(doc, out);

out.close();

 

 

-----------------------------------解析XML-----------------------------------------------------

 

//dom4j解析返回的XML字符串

Document doc = DocumentHelper.parseText(returnValue); 

Element rootElt = doc.getRootElement(); // 获取根节点

// 得到开发者类型

int developerType = Integer.valueOf(rootElt.elementText("developerType"));

Iterator<?> iter = rootElt.elementIterator("developerInfo"); 

SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd"); 

if(developerType==1){ //说明是个人开发者

while (iter.hasNext()) { // 遍历developerInfo节点

Element recordEle = (Element) iter.next();

//取出对应PersonDev类的属性

String developerId = recordEle.elementTextTrim("developerId"); 

String developerName = recordEle.elementTextTrim("developerName");

String nickName = recordEle.elementTextTrim("nickName"); 

String mobile = recordEle.elementTextTrim("mobile"); 

String email = recordEle.elementTextTrim("email"); 

String idType = recordEle.elementTextTrim("idType"); 

String idNumber = recordEle.elementTextTrim("idNumber"); 

String idCopy = recordEle.elementTextTrim("idCopy"); 

String address = recordEle.elementTextTrim("address"); 

String zipCode = recordEle.elementTextTrim("zipCode"); 

String accountName = recordEle.elementTextTrim("accountName"); 

String bankName = recordEle.elementTextTrim("bankName"); 

String bankAreaName = recordEle.elementTextTrim("bankAreaName");

String bankCityName = recordEle.elementTextTrim("bankCityName"); 

String bankAccount = recordEle.elementTextTrim("bankAccount"); 

String accountCopy = recordEle.elementTextTrim("accountCopy"); 

personDev = new PersonDev(Integer.valueOf(developerId),  developerType,            developerName,

nickName,  mobile,  email,Integer.valueOf(idType),

idNumber,  idCopy,  address,  zipCode,

accountName,  bankName,  bankAreaName,

bankCityName,  bankAccount,  accountCopy) ;

//取出对应Audition(审核情况)下的属性

String auditionResult = recordEle.elementTextTrim("auditionResult"); // 拿到developerInfo节点下的子节点对应值

String reasons = recordEle.elementTextTrim("reasons"); // 拿到developerInfo节点下的子节点对应值

long auditionDate =new Long(recordEle.elementTextTrim("auditionDate")) ; // 拿到developerInfo节点下的子节点对应值

Date d = sim.parse(sim.format(auditionDate));

audition = new Audition( Integer.valueOf(auditionResult),  reasons, d);

 

你可能感兴趣的:(xml)