dom4j 解析带命名空间的XML

1 篇文章 0 订阅
1 篇文章 0 订阅

//既然是使用DOM4J解析,当然需要在工程中把dom4j_1.6.1.jar包加载进去

/**解析的result.xml文件内容如下**/

<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body>
  <GetNewDataResponse xmlns="sobey.com.LBControlService/">
   <GetNewDataResult>
    <RtnValue>
     <ClientState IP="172.21.40.99" LASTTIME="2012-07-06 15:50:58" />
     <GenaralInfo DeviceID="470" Status="0" DataCaptureIP="xw-netmanage-2" DataCaptureTime="2012-07-06 15:23:20" >
       <key> 22 </key>
       <key> 2 3 </key>
     </GenaralInfo>
     <GenaralInfo DeviceID="470" Status="1" DataCaptureIP="xw-netmanage-2" DataCaptureTime="2012-07-06 15:23:22" >
     <key> 24 </key>
     </GenaralInfo>
     <GenaralInfo DeviceID="469" Status="1" DataCaptureIP="xw-netmanage-2" DataCaptureTime="2012-07-06 15:24:04" />
    </RtnValue>
   </GetNewDataResult>
  </GetNewDataResponse>
 </soap:Body>
</soap:Envelope>

//解析的java方法如下,其中解析带命名空间的xml方式有3种,这里列举两种常用的

package test;

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();
   System.out.println(StatusValue);
   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());
     }
    }
   }
   System.out.println("**********************************************************************");
  }
 }
 //取出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());
     }
    }
   }
  }
 }
}
工程截图如下:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值