`
zxs19861202
  • 浏览: 909410 次
  • 性别: Icon_minigender_1
  • 来自: 湖北—》上海
社区版块
存档分类
最新评论

Dom4j解析节点带前缀的XML文档(命名空间)

阅读更多

import java.io.StringReader;
import java.util.HashMap;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
import org.xml.sax.InputSource;

 


public class Test {
 
 
 //获取document对象
  public Document getDocument(String xml) throws DocumentException
  {
   StringReader read = new StringReader(xml);
      //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
      InputSource source = new InputSource(read);
      //创建一个新的SAXBuilder
      SAXReader sb = new SAXReader();
    
          //通过输入源构造一个Document
      Document doc = sb.read(source);
          //取的根元素
      return doc;
  }
 
  //获取带有命名空间的节点
  public Element getDestElement(Document doc)
  {
   HashMap<String,String> xmlMap = new HashMap<String,String>();
   xmlMap.put("tns","
http://www.99bill.com/schema/fo/settlement");
  
   XPath xpath=doc.createXPath("//tns:status"); //要获取哪个节点,改这里就可以了
   xpath.setNamespaceURIs(xmlMap);
   return (Element)xpath.selectSingleNode(doc);
  }


 
 public void jie()
 {
  String b=
   "<tns:batchid-query-response xmlns:ns0='http://www.99bill.com/schema/commons' xmlns:ns1='http://www.99bill.com/schema/fo/commons' xmlns:tns='http://www.99bill.com/schema/fo/settlement'>"+
     "<tns:response-header>"+
       "<tns:version xmlns:tns='http://www.99bill.com/schema/fo/commons'>"+
         "<ns0:version>1.0.1</ns0:version>"+
         "<ns0:service>fo.api.query</ns0:service>"+
       "</tns:version>"+
       "<ns1:time>20100811160633</ns1:time>"+
     "</tns:response-header>"+
     "<tns:response-body>"+
       "<tns:query-condition>"+
         "<tns:batch-no>SZ0806003</tns:batch-no>"+
         "<tns:page>1</tns:page>"+
         "<tns:page-size>20</tns:page-size>"+
         "<tns:list-flag>0</tns:list-flag>"+
       "</tns:query-condition>"+
       "<tns:total-page>1</tns:total-page>"+
       "<tns:total-cnt>2</tns:total-cnt>"+
       "<tns:batchList>"+
         "<tns:payer-acctCode>1001162953701</tns:payer-acctCode>"+
         "<tns:batch-no>SZ0806003</tns:batch-no>"+
         "<tns:apply-date>20100806103600</tns:apply-date>"+
         "<tns:name>大批量结算产品测试账户005</tns:name>"+
         "<tns:total-amt>10000</tns:total-amt>"+
         "<tns:total-cnt>2</tns:total-cnt>"+
         "<tns:fee-type>1</tns:fee-type>"+
         "<tns:cur>RMB</tns:cur>"+
         "<tns:checkAmt-cnt>0</tns:checkAmt-cnt>"+
         "<tns:batch-fail>1</tns:batch-fail>"+
         "<tns:recharge-type>0</tns:recharge-type>"+
         "<tns:auto-refund>0</tns:auto-refund>"+
         "<tns:phoneNote-flag>0</tns:phoneNote-flag>"+
         "<tns:merchant-memo1>memo1</tns:merchant-memo1>"+
         "<tns:merchant-memo2>memo2</tns:merchant-memo2>"+
         "<tns:merchant-memo3>memo3</tns:merchant-memo3>"+
         "<tns:status>111</tns:status>"+
         "<tns:order-seq-id>7368788</tns:order-seq-id>"+
         "<tns:total-applySucc-amt>10000</tns:total-applySucc-amt>"+
         "<tns:total-applySucc-cnt>2</tns:total-applySucc-cnt>"+
         "<tns:total-fee>1000</tns:total-fee>"+
         "<tns:finishPay-date>20100806105604</tns:finishPay-date>"+
         "<tns:memo/>"+
         "<tns:pay2bank-list>"+
           "<tns:pay2bank-result>"+
             "<tns:apply-date>20100806103600</tns:apply-date>"+
             "<tns:end-date>20100806105604</tns:end-date>"+
             "<tns:order-seq-id>7369556</tns:order-seq-id>"+
             "<tns:fee>500</tns:fee>"+
             "<tns:status>112</tns:status>"+
             "<tns:error-code>9113</tns:error-code>"+
             "<tns:error-msg>客户姓名不符</tns:error-msg>"+
             "<tns:bank-error-code/>"+
             "<tns:bank-error-msg/>"+
             "<tns:pay2bank>"+
               "<ns1:merchant-id>20100806_005</ns1:merchant-id>"+
               "<ns1:memo>快钱交易备注</ns1:memo>"+
               "<ns1:bank-purpose>理赔</ns1:bank-purpose>"+
               "<ns1:bank-memo>银行交易备注</ns1:bank-memo>"+
               "<ns1:payee-note>付款</ns1:payee-note>"+
               "<ns1:payee-mobile>13891819014</ns1:payee-mobile>"+
               "<ns1:payee-email>zhenzhen.sun@99bill.com</ns1:payee-email>"+
               "<ns1:period/>"+
               "<ns1:merchant-memo1>1</ns1:merchant-memo1>"+
               "<ns1:merchant-memo2>2</ns1:merchant-memo2>"+
               "<ns1:merchant-memo3>3</ns1:merchant-memo3>"+
             "</tns:pay2bank>"+
           "</tns:pay2bank-result>"+
         
         "</tns:pay2bank-list>"+
       "</tns:batchList>"+
     "</tns:response-body>"+
   "</tns:batchid-query-response>";
  
  
  try {
   Document doc=this.getDocument(b);
   Element el=this.getDestElement(doc);
   System.out.println("*********"+el.getText());
   
   
  } catch (DocumentException e) {
   // TODO 自动生成 catch 块
   e.printStackTrace();
  }


 }
 

 public static void main(String[] args) {
  
  Test t=new Test();
  t.jie();
  

 }

}

分享到:
评论
1 楼 ohano_javaee 2013-10-08  
如果属性也有前缀,怎么读取?

相关推荐

Global site tag (gtag.js) - Google Analytics