dom4j解析带命名空间的xml
需要被解析的xml文件如下:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 4 <soap:Body> 5 <GetNewDataResponse xmlns="sobey.com.LBControlService/"> 6 <GetNewDataResult> 7 <RtnValue> 8 <ClientState IP="172.21.40.99" LASTTIME="2012-07-06 15:50:58" /> 9 <GenaralInfo DeviceID="470" Status="0" DataCaptureIP="xw-netmanage-2" DataCaptureTime="2012-07-06 15:23:20" > 10 <key> 22 </key> 11 <key> 2 3 </key> 12 </GenaralInfo> 13 <GenaralInfo DeviceID="470" Status="0" DataCaptureIP="xw-netmanage-2" DataCaptureTime="2012-07-06 15:23:22" > 14 <key22> 24 </key22> 15 </GenaralInfo> 16 <GenaralInfo DeviceID="469" Status="0" DataCaptureIP="xw-netmanage-2" DataCaptureTime="2012-07-06 15:24:04" > 17 <key> 55 </key> 18 </GenaralInfo> 19 </RtnValue> 20 </GetNewDataResult> 21 </GetNewDataResponse> 22 </soap:Body> 23 </soap:Envelope>
注意:需要添加包dom4j-1.6.1.jar,jaxen-1.1-beta-9.jar
package src.liweiTest.dom4j; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class TestDom4j { public static void main(String[] args) throws Exception { System.out.println("test1()***********************************************"); test1(); System.out.println("test2()***********************************************"); test2(); } // 取xml中的域名解析 public static void test1() throws DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(TestDom4j.class.getResourceAsStream("/result.xml")); Element e = document.getRootElement(); // 取节点名字是GenaralInfo而且其中属性DeviceID=470的元素的集合 // @DeviceID=470 表示的是属性 local-name()='GenaralInfo'表示的是节点名称 namespace-uri()表示域名空间 List<Element> list = e.selectNodes("//*[@DeviceID=470 and local-name()='GenaralInfo' and namespace-uri()='sobey.com.LBControlService/']"); for (Element ee : list) { // ee.asXML()把Element 元素转换成String // System.out.println(ee.asXML()); // 取得元素中Status属性值为0的子元素的值 Attribute Status = ee.attribute("Status"); String StatusValue = Status.getValue(); if (Status != null) { if ("0".equals(StatusValue)) { List<Element> keys = ee.selectNodes(".//*[local-name()='key' and namespace-uri()='sobey.com.LBControlService/']"); for (Element key : keys) { System.out.println("key.value is :" + key.getTextTrim()); } } } } } // 取出xml中的空间名称,并且给域名赋予别名 public static void test2() throws DocumentException { SAXReader reader = new SAXReader(); Document d = reader.read(TestDom4j.class.getResourceAsStream("/result.xml")); Document document = DocumentHelper.parseText(d.asXML()); // ee.asXML()把Element 元素转换成String System.out.println(d.asXML()); Element root = document.getRootElement(); root.addNamespace("s", "sobey.com.LBControlService/"); List<Element> list = root.selectNodes("//s:GenaralInfo"); for (Element e : list) { // System.out.println(e.asXML()); // 取得元素中Status属性值为0的子元素的值 Attribute Status = e.attribute("Status"); // 获取属性的值 if (Status != null) { String StatusValue = Status.getValue(); if ("0".equals(StatusValue)) { // e.selectNodes(".//s:key")获取当前节点下元素key的值;e.selectNodes("//s:key")获取所有元素为key的值;记得加“//s:” List<Element> keys = e.selectNodes(".//s:key"); for (Element key : keys) { System.out.println("key.value is :" + key.getTextTrim()); } } } } } }