dom4j解析xml文件和字符串

解析xml字符串,如下

Java代码   收藏代码
  1. public static void main(String[] args) {  
  2.         //System.out.println("cccc");  
  3.         String fStr="<?xml version='1.0' encoding='UTF-8'?>" +  
  4.                 "<ROOT test='test123' cod='cod123'><Name>AAA</Name><Number>BBB</Number>" +  
  5.                 "<Recording>http://10.15.57.174/wav/2008/10/29/WG37100/ext37102/10.15.57.71!1~R!10292008_064002!37102!67256479!Ext!NA!1179371583!R.wav</Recording>" +  
  6.                 "<Orders>有</Orders></ROOT>";  
  7.         try {  
  8.             SAXReader reader = new SAXReader();  
  9.              Document doc;   
  10.              doc = DocumentHelper.parseText(fStr);   
  11.   
  12.             //Document doc = reader.read(ffile); //读取一个xml的文件  
  13.             Element root = doc.getRootElement();  
  14.             Attribute testCmd= root.attribute("test");  
  15.             System.out.println(testCmd.getName()+"-***--"+testCmd.getValue());                         
  16.             Element eName = root.element("Name");  
  17.             System.out.println("节点内容*--"+eName.getTextTrim());        
  18.           
  19.         } catch (Exception e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  

 

 

解析xml文件,xml文件如下

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <students>  
  3.     <student age="25">  
  4.         <name>AAA</name>  
  5.         <collage leader='班长'>xxx学校</collage>  
  6.         <telephone>13728704450</telephone>  
  7.         <notes>男,1958年出生</notes>  
  8.     </student>  
  9.     <student age="21">  
  10.         <name>BBB</name>  
  11.         <collage leader='学生'>YYY学校</collage>  
  12.         <telephone>1372sadgasg4450</telephone>  
  13.         <notes>男,1968年出生,出生地长沙</notes>  
  14.     </student>  
  15.     <student age="29">  
  16.         <name>CCC</name>  
  17.         <collage leader='班主任'>北京大学</collage>  
  18.         <telephone>13828704450</telephone>  
  19.         <notes>女,漂亮</notes>  
  20.     </student>  
  21. </students>  

 

解析的代码如下

 

Java代码   收藏代码
  1. public void iterateWholeXML(String filename, HashMap<String, String> hm) {  
  2.         SAXReader saxReader = new SAXReader();  
  3.         try {  
  4.             Document document = saxReader.read(new File(filename));  
  5.             Element root = document.getRootElement();  
  6.             // 用于记录学生编号的变量  
  7.             int num = -1;  
  8.             // 遍历根结点(students)的所有孩子节点(肯定是student节点)  
  9.             for (Iterator iter = root.elementIterator(); iter.hasNext();) {  
  10.                 Element element = (Element) iter.next();  
  11.                 num++;  
  12.                 // 获取person节点的age属性的值  
  13.                 Attribute ageAttr = element.attribute("age");  
  14.                 if (ageAttr != null) {  
  15.                     String age = ageAttr.getValue();  
  16.                     if (age != null && !age.equals("")) {  
  17.                         hm.put(element.getName() + "-" + ageAttr.getName()  
  18.                                 + num, age);  
  19.                     } else {  
  20.                         hm.put(element.getName() + "-" + ageAttr.getName()  
  21.                                 + num, "20");  
  22.                     }  
  23.                 } else {  
  24.                     hm.put(element.getName() + "-age" + num, "20");  
  25.                 }  
  26.                 // 遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理  
  27.                 for (Iterator iterInner = element.elementIterator(); iterInner  
  28.                         .hasNext();) {  
  29.                     Element elementInner = (Element) iterInner.next();  
  30.                     if (elementInner.getName().equals("collage")) {  
  31.                         hm.put(elementInner.getName() + num, elementInner  
  32.                                 .getText());  
  33.                         // 获取college节点的leader属性的值  
  34.                         Attribute leaderAttr = elementInner.attribute("leader");  
  35.                         if (leaderAttr != null) {  
  36.                             String leader = leaderAttr.getValue();  
  37.                             if (leader != null && !leader.equals("")) {  
  38.                                 hm.put(elementInner.getName() + "-"  
  39.                                         + leaderAttr.getName() + num, leader);  
  40.                             } else {  
  41.                                 hm.put(elementInner.getName() + "-"  
  42.                                         + leaderAttr.getName() + num, "leader");  
  43.                             }  
  44.                         } else {  
  45.                             hm.put(elementInner.getName() + "-leader" + num,  
  46.                                     "leader");  
  47.                         }  
  48.                     } else {  
  49.                         hm.put(elementInner.getName() + num, elementInner  
  50.                                 .getText());  
  51.                     }  
  52.                 }  
  53.             }  
  54.         } catch (DocumentException e) {  
  55.             e.printStackTrace();  
  56.         }  
  57.     }  

你可能感兴趣的:(dom4j)