解决dom4j无法解析xml命名空间的问题

package com.lixueli.test;

import java.util.HashMap;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;

public class ParseXml {
 public static void main(String [] args) throws DocumentException{
       ParseXml p=new ParseXml();
       p.test2();
       //p.test3();

	 }
   public void test2() throws DocumentException{
	  String xml= "<?xml version='1.0' encoding='GBK'?>" +
			  		"<DECSystem version='1.0' xmlns='http://www.chinatax.gov.cn/tirip/dataspec'>" +
			  		"<EventIds>sssss</EventIds>"+
			  		"<Service>" +
			  			"<EventId>CTais1.Get.WFWZJBXX</EventId>" +
			  		"</Service>" +
			  		"<DataPackage>" +
			  		  "<DataContent>" +
			  		  	"<Id></Id>" +
			  		    "<Content><![CDATA[<taxML version='1.0' name='CTais1.Get.WFWZJBXX' cnName='CTais获取违法违章基本信息'>" +
			  		    	"<CTais1_Get_WFWZJBXX>" +
			  		    	"<Item>" +
			  		    		"<NSRSBH>460200561120313</NSRSBH>" +
			  		    		"<NSRMC>三亚荔枝沟春生副食商行</NSRMC>" +
			  		    		"<DJZCLX_DL_DM>400</DJZCLX_DL_DM>" +
			  		    		"<DJZCLX_DL_MC>个体经营</DJZCLX_DL_MC>" +
			  		    	"</Item>" +
			  		    	"</CTais1_Get_WFWZJBXX></taxML>]]>" +
			  		    	"</Content>" +
			  		  "</DataContent>" +
			  		  "</DataPackage>" +
			  		  "<ReturnState>" +
			  		  	"<ReturnCode>000</ReturnCode>" +
			  		  	"<ReturnMessage>执行CTais获取违法违章基本信息成功</ReturnMessage>" +
			  		  "</ReturnState>" +
			  	   "</DECSystem>";

	  Document xmlString = DocumentHelper.parseText(xml);
	  
	  
	  HashMap xmlMap = new HashMap();   
      xmlMap.put("mo","http://www.chinatax.gov.cn/tirip/dataspec");   
      XPath x = xmlString.createXPath("//mo:Content");   
      x.setNamespaceURIs(xmlMap);            
      Element valueElement = (Element)x.selectSingleNode(xmlString);   
      System.out.println(valueElement.getText());   

	  
	    
//	  System.out.println("Query Xml = " + xmlString.asXML());
	    
   }
   
   public void test3() throws DocumentException{
		 String query_data = 
		      "<?xml version='1.0' encoding='GBK'?>" +
		      "<Query>" +
		      "  <Result>00000</Result>" +
		      "  <Message>" +
		      "    <MId>12001</MId>" +
		      "    <Date>2006/08/07 12:23:40</Date>" +
		      "    <Report>" +
		      "      <ID>0010571053</ID>" +
		      "      <Birthday>2000/01/01</Birthday>" + 
		      "</Report>" +
		      "</Message>" +
		      "</Query>" ; 
		    
		    Document xmlDocument = DocumentHelper.parseText(query_data);
		    
		    // 可以使用 Xpath 来读取各节点的资讯
		    System.out.println("Result = " + xmlDocument.selectSingleNode("Query/Result").getText());
		    System.out.println("MId = " + xmlDocument.selectSingleNode("Query/Message/MId").getText());
		    System.out.println("Date = " + xmlDocument.selectSingleNode("Query/Message/Date").getText());
		    System.out.println("ID = " + xmlDocument.selectSingleNode("Query/Message/Report/ID").getText());
		    System.out.println("Birthday = " + xmlDocument.selectSingleNode("Query/Message/Report/Birthday").getText());
		    
		    System.out.println("Query Xml = " + xmlDocument.asXML());
   }
 
 
}

 

你可能感兴趣的:(xml,资讯)