存档

2011年1月 的存档

dom4j带命名空间的xml操作

2011年1月18日 admin 1 条评论

xml文件—-myXML.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 <?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源文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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字符串的时候在属性前加个“@”就行了。

1
2
3
4
5
6
7
 <?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文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    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();  
         }  
     }  
 }
分类: Java 标签: ,