java中使用dom4j对带有命名空间的xml文件进行解析

 
    
  1. import org.dom4j.Document; 
  2. import org.dom4j.DocumentHelper; 
  3. import org.dom4j.DocumentException; 
  4. import org.dom4j.Element; 
  5. import org.dom4j.XPath; 
  6.  
  7. import java.util.*; 
  8.  
  9. /** 
  10. *  Java解析XML元素 
  11. */ 
  12. public class XML { 
  13.  
  14.     //要测试的xml对象,带有命名空间 
  15.     static String str = "" + 
  16.                 "" + 
  17.                 "12345678" + 
  18.                 "20130328155409100477" + 
  19.                 "66666888" + 
  20.                 "test" + 
  21.                 "378地方378" + 
  22.                 ""
  23.     //要测试的xml对象,没有命名空间 
  24.     static String str1 = "" + 
  25.                 "" + 
  26.                 "12345678" + 
  27.                 "20130328155409100477" + 
  28.                 "66666888" + 
  29.                 "test" + 
  30.                 "378地方378" + 
  31.                 "";   
  32.      
  33.     //传统方法一步一步解析带有命名空间的XML文件 
  34.     @SuppressWarnings("unchecked"
  35.     private static void test() throws DocumentException { 
  36.         List> seatlist = new ArrayList>(); 
  37.              
  38.         Document document = DocumentHelper.parseText(str); 
  39.         Element root = document.getRootElement(); 
  40.         Element query = root.element("query"); 
  41.         List workgroups = query.elements("workgroup"); 
  42.         for(Element workgroup : workgroups) { 
  43.             Element seatsElement = workgroup.element("seats"); 
  44.             List seats = seatsElement.elements("seat"); 
  45.             for(Element seat : seats) { 
  46.                 Map seatmap = new HashMap(); 
  47.                 String jid = seat.elementText("jid"); 
  48.                 String seatname = seat.elementText("name"); 
  49.                 String seatnumber = seat.elementText("seatnumber"); 
  50.                 String sessioncount = seat.elementText("sessioncount"); 
  51.                 seatmap.put("jid",jid); 
  52.                 seatmap.put("seatname",seatname); 
  53.                 seatmap.put("seatnumber",seatnumber); 
  54.                 seatmap.put("sessioncount",sessioncount); 
  55.                 seatlist.add(seatmap); 
  56.                 System.out.println(seatmap); 
  57.             } 
  58.         } 
  59.         System.out.println(seatlist); 
  60.     } 
  61.      
  62.     //Xpath方法解析没有命名空间的XML文件 
  63.     @SuppressWarnings("unchecked"
  64.     private static void test1() throws DocumentException { 
  65.         List> seatlist = new ArrayList>(); 
  66.  
  67.         Document document = DocumentHelper.parseText(str1); 
  68.         List seats = document.selectNodes("//seat"); 
  69.           for (int i = 0; i < seats.size(); i++) { 
  70.             Map seatmap = new HashMap(); 
  71.             Element seat = (Element) seats.get(i); 
  72.             String jid = seat.elementText("jid"); 
  73.             String seatname = seat.elementText("name"); 
  74.             String seatnumber = seat.elementText("seatnumber"); 
  75.             String sessioncount = seat.elementText("sessioncount"); 
  76.             seatmap.put("jid",jid); 
  77.             seatmap.put("seatname",seatname); 
  78.             seatmap.put("seatnumber",seatnumber); 
  79.             seatmap.put("sessioncount",sessioncount); 
  80.             seatlist.add(seatmap); 
  81.           } 
  82.         System.out.println(seatlist); 
  83.     } 
  84.      
  85.     //带有命名空间的XML文件,Xpath的解析方法 
  86.     @SuppressWarnings("unchecked"
  87.     private static void test2() throws DocumentException { 
  88.         List> seatlist = new ArrayList>(); 
  89.          
  90.         Map map = new HashMap(); 
  91.         map.put("getonlineseat","http://te.com/resource/cti/getonlineseat"); 
  92.         Document document = DocumentHelper.parseText(str); 
  93.         XPath x = document.createXPath("//getonlineseat:seat"); 
  94.         x.setNamespaceURIs(map); 
  95.         List nodelist = x.selectNodes(document);    
  96.           for (int i = 0; i < nodelist.size(); i++) { 
  97.             Map seatmap = new HashMap(); 
  98.             Element seat = (Element) nodelist.get(i); 
  99.             String jid = seat.elementText("jid"); 
  100.             String seatname = seat.elementText("name"); 
  101.             String seatnumber = seat.elementText("seatnumber"); 
  102.             String sessioncount = seat.elementText("sessioncount"); 
  103.             seatmap.put("jid",jid); 
  104.             seatmap.put("seatname",seatname); 
  105.             seatmap.put("seatnumber",seatnumber); 
  106.             seatmap.put("sessioncount",sessioncount); 
  107.             seatlist.add(seatmap); 
  108.           } 
  109.         System.out.println(seatlist); 
  110.     } 
  111.  
  112.     //主函数 
  113.     public static void main(String args[]) throws DocumentException { 
  114.         System.out.println("-----------test()方法------------"); 
  115.         test(); 
  116.         System.out.println("-----------test1()方法------------"); 
  117.         test1(); 
  118.         System.out.println("-----------test2()方法------------"); 
  119.         test2(); 
  120.     }