public static void main(String[] args){
//解析soap.xml字符串,从return开始截取
String str = "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:uploadBatchCreditFileResponse xmlns:ns2="http://webservice.creditreport.p2p.sino.com/"><return>[{"errors":"文件名称重复","success":false}]</return></ns2:uploadBatchCreditFileResponse></soap:Body></soap:Envelope>";
System.out.println("escape: \n" + HtmlUtils.htmlUnescape(str)); // 可以实现HTML标签及转义字符之间的转换。
xmlElements(str);//静态的可以直接调用
}
public static void xmlElements(String str) throws Exception {
String res = file;
Document personDoc;
try {
personDoc = new SAXReader().read(new StringReader(new String(res.getBytes("UTF-8"), "UTF-8")));//编码格式,根据自己项目设计
Element rootElt = personDoc.getRootElement(); // 获取根节点 Envelope
System.out.println("根节点:" + rootElt.getName()); // 拿到根节点的名称
Iterator body = rootElt.elementIterator("Body");
while (body.hasNext()) { //迭代Body节点里面子节点
Element recordEless = (Element) body.next();
System.out.println(recordEless);
Iterator<Element> cdGasFyResponse = recordEless.elementIterator();
while (cdGasFyResponse.hasNext()) {
Element e = cdGasFyResponse.next();//获取body next个节点uploadBatchCreditFileResponse
String returnIt = e.elementText("return"); //return 里面的文本
String s = returnIt.substring(1, returnIt.length()-1);//截取[头和]尾 [{"":""}]
JSONObject jsStr = JSONObject.fromObject(s); //将字符串转换成json对象
System.out.println(jsStr);
String errors = jsStr.getString("errors");//获取得到的errors值
String success = jsStr.getString("success");
System.out.println(errors+success);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}