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

解决dom4j无法解析xml命名空间的问题
困扰我几周的dom4j无法解析xml命名空间的问题近日得以解决,如果这个问题也正在困扰你,看看下文也许能给你一些启发

xml文件----myXML.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>  
<MyXML xmlns="http://www.ttt.com/ttt-TrdInfo-1-0" xmlns:x="http://www.ttt.com/ttt/metadata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="res286.xsd">  
<Hdr>  
    <ReqId>001</ReqId>  
    <Tid>1002</Tid>  
    <Cid>500</Cid>  
    <user>cuishen</user>  
    <Mname>supermarket</Mname>   
    <pwd>543200210</pwd>  
</Hdr>  
<Car>  
    <Flg>T</Flg>  
    <Cod>ccc</Cod>  
    <Door>kkk</Door>  
    <mktId>b01</mktId>  
    <Key>  
        <KeyID>t01</KeyID>  
    </Key>  
</Car>  
</MyXML> 

<?xml version="1.0" encoding="UTF-8"?>
<MyXML xmlns="http://www.ttt.com/ttt-TrdInfo-1-0" xmlns:x="http://www.ttt.com/ttt/metadata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="res286.xsd">
<Hdr>
 <ReqId>001</ReqId>
 <Tid>1002</Tid>
 <Cid>500</Cid>
 <user>cuishen</user>
 <Mname>supermarket</Mname>
 <pwd>543200210</pwd>
</Hdr>
<Car>
 <Flg>T</Flg>
 <Cod>ccc</Cod>
 <Door>kkk</Door>
 <mktId>b01</mktId>
 <Key>
  <KeyID>t01</KeyID>
 </Key>
</Car>
</MyXML>


下面是用dom4j解析上面xml文件的java源文件

---ReadMyXML.java
Java代码
import java.io.File;  
import java.util.List;  
import java.util.Map;  
import java.util.HashMap;  
 
import org.dom4j.Document;  
import org.dom4j.Element;  
import org.dom4j.XPath;  
import org.dom4j.Attribute;  
import org.dom4j.io.SAXReader;  
import org.dom4j.DocumentException;  
 
public class ReadMyXML{  
    public static void main(String args[]){  
        File xmlFile = new File("c:/myXML.xml");  
        SAXReader xmlReader = new SAXReader();  
        try{  
            Document document = xmlReader.read(xmlFile);  
            ///*测试代码    适用于读取xml的节点  
            HashMap xmlMap = new HashMap();  
            xmlMap.put("mo","http://www.ttt.com/ttt-TrdInfo-1-0");  
            XPath x = document.createXPath("//mo:ReqId");  
            x.setNamespaceURIs(xmlMap);           
            Element valueElement = (Element)x.selectSingleNode(document);  
            System.out.println(valueElement.getText());  
            //*/  
        }catch(DocumentException e){  
            e.printStackTrace();  
        }  
    }  

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.Attribute;
import org.dom4j.io.SAXReader;
import org.dom4j.DocumentException;

public class ReadMyXML{
 public static void main(String args[]){
  File xmlFile = new File("c:/myXML.xml");
  SAXReader xmlReader = new SAXReader();
  try{
   Document document = xmlReader.read(xmlFile);
   ///*测试代码    适用于读取xml的节点
   HashMap xmlMap = new HashMap();
   xmlMap.put("mo","http://www.ttt.com/ttt-TrdInfo-1-0");
   XPath x = document.createXPath("//mo:ReqId");
   x.setNamespaceURIs(xmlMap);   
   Element valueElement = (Element)x.selectSingleNode(document);
   System.out.println(valueElement.getText());
   //*/
  }catch(DocumentException e){
   e.printStackTrace();
  }
 }
}


上面就是运用dom4j解析带命名空间的xml文件的节点的例子,只要给XPath设置默认的命名空间就行了,这个xml文件尽管定义了其他命名空间,但是没有用到它,所以不必管它,那个HashMap里的键是随便定义的字符串,值就是默认的命名空间对应的字符串。document.createXPath()里传的参数是要读取的节点的XPath,即“//”+ HashMap里的键名 + “:”+ 要读取的节点名组成的字符串,简单吧,后面怎么做我就不用说了吧^_^
如果要读取的是xml文件里的属性该怎么办呢,不用急,看看下面的例子你就明白了,原理一样,只要在造XPath字符串的时候在属性前加个“@”就行了。

xml文件----myXML2.xml
Java代码
<?xml version="1.0" encoding="UTF-8"?>  
<MyXML xmlns="http://www.ttt.com/ttt-TrdInfo-1-0" xmlns:x="http://www.ttt.com/ttt/metadata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="res286.xsd">  
<Hdr ReqId="001" Tid="1002" Cid="500" user="cuishen" Mname="supermarket" pwd="543200210"/>  
<Car Flg="T" Cod="ccc" Door="kkk" mktId="b01">  
<Key KeyID="t01"/>  
</Car>  
</MyXML> 

<?xml version="1.0" encoding="UTF-8"?>
<MyXML xmlns="http://www.ttt.com/ttt-TrdInfo-1-0" xmlns:x="http://www.ttt.com/ttt/metadata.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="res286.xsd">
<Hdr ReqId="001" Tid="1002" Cid="500" user="cuishen" Mname="supermarket" pwd="543200210"/>
<Car Flg="T" Cod="ccc" Door="kkk" mktId="b01">
<Key KeyID="t01"/>
</Car>
</MyXML>


解析上面xml文件的java文件如下
---ReadMyXML2.java
Java代码
import java.io.File;  
import java.util.List;  
import java.util.Map;  
import java.util.HashMap;  
 
import org.dom4j.Document;  
import org.dom4j.Element;  
import org.dom4j.XPath;  
import org.dom4j.Attribute;  
import org.dom4j.io.SAXReader;  
import org.dom4j.DocumentException;  
 
public class ReadMyXML2{  
    public static void main(String args[]){  
        File xmlFile = new File("c:/myXML2.xml");  
        SAXReader xmlReader = new SAXReader();  
        try{  
            Document document = xmlReader.read(xmlFile);  
            ///*测试代码  解析xml的属性  
            HashMap xmlMap = new HashMap();  
            xmlMap.put("mo","http://www.ttt.com/ttt-TrdInfo-1-0");  
            XPath x = document.createXPath("//mo:Hdr/@ReqId");  
            x.setNamespaceURIs(xmlMap);  
            Attribute valueAttribute = (Attribute)x.selectSingleNode(document);  
            System.out.println(valueAttribute.getText());  
            //*/  
        }catch(DocumentException e){  
            e.printStackTrace();  
        }  
    }  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值