dom4j解析接口使用SOAP传递的xml
xml 文件的格式类型:
<?xml version="1.0" encoding="utf-8"?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <AccountManagementResponse> <Request_ID>1414475429249</Request_ID> <Login_ID>CHNH000011</Login_ID> <Action>C</Action> <Status>failed</Status> <Reason>Login ID already existed</Reason> </AccountManagementResponse> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
普通的测试Demo
/** * 通过SaxReader方式解析Xml文件 * @param filename * @throws Exception */ public static void parseXmlSax(String filename) throws Exception{ Document document = null; SAXReader saxReader = new SAXReader();
// 此处read也可以读取流文件 document = saxReader.read(new File(filename)); Element root = document.getRootElement(); System.out.println("根节点 为 : " + root.getName()); Element elm = root.element("Body"); // 获取Body下的所有子节点 List<?> bodyChild = elm.elements(); // 遍历所有的AccountManagementResponse当前节点 for(Iterator<?> it = bodyChild.iterator();it.hasNext();){ Element elm1= (Element) it.next(); System.out.println(elm1.getName()); List<?> responseChild = elm1.elements(); // 遍历AccountManagementResponse节点下的所有节点 for(Iterator<?> it1 = responseChild.iterator();it1.hasNext();){ Element elm2= (Element) it1.next(); // 取得最后节点下的名称和值 System.out.println(elm2.getName() + " ===== " + elm2.getText()); } } }