调用Web Service返回了一个XML字符串,XML字符串中是多个用户的基本信息,通过下面的代码可以实现对XML结果的解析:
public static List<OADTO> getResult(String xml) {
Document doc;
List<OADTO> oadtos= new ArrayList<>();
try {
// 将字符串转为XML
doc = DocumentHelper.parseText(xml);
// 获取根节点
Element rootElt = doc.getRootElement();
Iterator ABK = rootElt.elementIterator("UserBeans");
while (ABK.hasNext()) {
OADTO oadto = new OADTO();
Element abkRecord = (Element) ABK.next();
//获取ABK节点下的所有节点
Iterator f = abkRecord.elementIterator();
while (f.hasNext()) {
Element itemAtr = (Element) f.next();
//获取需要的数据
oadto.setUserid(Integer.parseInt(itemAtr.attributeValue("userid")));
oadto.setEmail(itemAtr.attributeValue("email"));
oadto.setLoginid(itemAtr.attributeValue("loginid"));
oadto.setMobile(itemAtr.attributeValue("mobile"));
oadto.setWorkcode(itemAtr.attributeValue("workcode"));
oadto.setLastname(itemAtr.attributeValue("lastname"));
}
oadtos.add(oadto);
}
} catch (DocumentException e) {
e.printStackTrace();
}
return oadtos;
}