1.直接用xml字符串创建Document对象,并取得相应属性的值。
Document xmlDoc = DocumentHelper.parseText(requestXmlStr);//创建document对象
Element root = xmlDoc.getRootElement(); //取得根目录的Element对象
String optType = root.element("Msgbody").element("optType").getText(); //取得对应的属性值
String userCode = root.element("Msgbody").element("userCode").getText();
2.用xml文件创建Document对象,处理有循环属性的xml
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new File(
"E:/data/cityFlag/cityFlagData.xml"));
Element responseRoot = doc.getRootElement();
List<Element> list = responseRoot.elements();
for (int i = 0; i < list.size(); i++) {
Element e = list.get(i);
if (e.elementText("UserCode").equals(userCode)
&& e.elementText("optType").equals(optType)) {
responseXmlStr = e.elementText("cityFlag");
}
}