//处理返回值
Map<String, Object> params2 = this.readXml(result);
String retCode = (String)params2.get("retCode");
String retDesc = (String)params2.get("retDesc");
String retValue = (String)params2.get("retValue");
System.out.println(retCode + " " + retDesc + " " + retValue);
// 读取XML
@SuppressWarnings({ "rawtypes" })
public Map<String, Object> readXml(String strIn) {
String retCode = null;
String retDesc = null;
String retValue = null;
// 创建SAXReader的对象reader
SAXReader reader = new SAXReader();
try {
//String转InputStream
ByteArrayInputStream byteArrayInputStream = null;
try {
byteArrayInputStream = new ByteArrayInputStream(
strIn.getBytes("GBK"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = (InputStream)byteArrayInputStream;
// 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
Document document = reader.read(in);
//Document document = reader.read(new File("D://test.xml"));
// 通过document对象获取根节点bookstore
Element bookStore = document.getRootElement();
// 通过element对象的elementIterator方法获取迭代器
Iterator it = bookStore.elementIterator();
// 遍历迭代器,获取根节点中的信息(书籍)
while (it.hasNext()) {
//System.out.println("=====开始遍历某一本书=====");
Element book = (Element) it.next();
// 获取book的属性名以及 属性值
//List<Attribute> bookAttrs = book.attributes();
//for (Attribute attr : bookAttrs) {
// System.out.println("属性名:" + attr.getName() + "--属性值:"
// + attr.getValue());
//}
Iterator itt = book.elementIterator();
while (itt.hasNext()) {
Element bookChild = (Element) itt.next();
//System.out.println("节点名:" + bookChild.getName() + "--节点值:"
//+ bookChild.getStringValue());
if ("retCode".equals(bookChild.getName())) {
retCode = bookChild.getStringValue();
} else if ("retDesc".equals(bookChild.getName())) {
retDesc = bookChild.getStringValue();
} else if ("retValue".equals(bookChild.getName())) {
retValue = bookChild.getStringValue();
}
}
//System.out.println("=====结束遍历某一本书=====");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, Object> params = new HashMap<String, Object>();
params.put("retCode", retCode);
params.put("retDesc", retDesc);
params.put("retValue", retValue);
return params;
}