java读取xml指定标签的内容

  1. package com.fenuang.sec;  
  2.   
  3. import java.io.File;  
  4. import java.util.ArrayList;  
  5. import java.util.Collection;  
  6. import java.util.HashMap;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9.   
  10. import javax.print.Doc;  
  11.   
  12. import org.dom4j.Document;  
  13. import org.dom4j.Element;  
  14. import org.dom4j.io.SAXReader;  
  15.   
  16. import com.fenuang.demo4j.contest;  
  17.   
  18. public class trueuse {  
  19.   
  20.     public static void main(String[] args) {  
  21.         // TODO Auto-generated method stub  
  22.   
  23.         try {  
  24.             // 实例化一个类用于添加xml文件  
  25.             List<contest> list = new ArrayList<>();  
  26.   
  27.             SAXReader reader = new SAXReader();  
  28.             Document doc = reader.read(new File("./conf/test.xml"));  
  29.             // 读取指定标签  
  30.             Iterator<Element> eleit = doc.getRootElement().elementIterator("user");  
  31.   
  32.             ArrayList newlist = new ArrayList(); // 创建新集合  
  33.             while (eleit.hasNext()) {  
  34.   
  35.                 Element ele = eleit.next();  
  36.                 contest con = new contest();  
  37.                 con.setId(ele.attributeValue("id"));  
  38.                 con.setName(ele.elementText("name"));  
  39.                 con.setEmail(ele.elementText("email"));  
  40.                 con.setQq(ele.elementText("qq"));  
  41.                 con.setWife(ele.elementText("wife"));  
  42.                 list.add(con);  
  43.   
  44.                 Iterator it = list.iterator(); // 通过老集合获取迭代器  
  45.                 while (it.hasNext()) { // 遍历老集合  
  46.                     Object obj = it.next(); // 得到老集合的元素  
  47.                     if (!newlist.contains(obj)) {  
  48.                         newlist.add(obj);  
  49.                     }  
  50.                 }  
  51.   
  52.             }  
  53.   
  54.             System.out.println(list.get(0));  
  55.             // System.out.println(" =======");  
  56.             System.out.println(list.get(1));  
  57.             // System.out.println(" =======");  
  58.             System.out.println(list.get(2));  
  59.   
  60.         } catch (  
  61.   
  62.         Exception e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.   
  66.     }  
  67.   
  68. }  

下面是用于存储元素的类


[java]  view plain  copy
  1. package com.fenuang.demo4j;  
  2.   
  3. import org.omg.Messaging.SyncScopeHelper;  
  4.   
  5. public class contest {  
  6.     private String id;  
  7.     private String name;  
  8.     private String email;  
  9.     private String qq;  
  10.     private String wife;  
  11.   
  12.     /** 
  13.      * @param id 
  14.      * @param name 
  15.      * @param email 
  16.      * @param qq 
  17.      * @param wife 
  18.      */  
  19.     public contest(String id, String name, String email, String qq, String wife) {  
  20.         super();  
  21.         this.id = id;  
  22.         this.name = name;  
  23.         this.email = email;  
  24.         this.qq = qq;  
  25.         this.wife = wife;  
  26.     }  
  27.   
  28.     /** 
  29.      * @param id 
  30.      * @param name 
  31.      * @param email 
  32.      * @param qq 
  33.      * @param wife 
  34.      */  
  35.     public contest() {  
  36.   
  37.     }  
  38.   
  39.     public String getId() {  
  40.         return id;  
  41.     }  
  42.   
  43.     public void setId(String id) {  
  44.         this.id = id;  
  45.     }  
  46.   
  47.     public String getName() {  
  48.         return name;  
  49.     }  
  50.   
  51.     public void setName(String name) {  
  52.         this.name = name;  
  53.     }  
  54.   
  55.     public String getEmail() {  
  56.         return email;  
  57.     }  
  58.   
  59.     public void setEmail(String email) {  
  60.         this.email = email;  
  61.     }  
  62.   
  63.     public String getQq() {  
  64.         return qq;  
  65.     }  
  66.   
  67.     public void setQq(String qq) {  
  68.         this.qq = qq;  
  69.     }  
  70.   
  71.     public String getWife() {  
  72.         return wife;  
  73.     }  
  74.   
  75.     public void setWife(String wife) {  
  76.         this.wife = wife;  
  77.     }  
  78.   
  79.     @Override  
  80.     public String toString() {  
  81.         // TODO Auto-generated method stub  
  82.         return this.email + "--" + this.id + "--" + this.name + "--" + this.qq + "--" + this.wife+" ";  
  83.   
  84.           
  85.     }  
  86.   
  87. }  

下面是要被读取的xml文件


[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <contestList>  
  4.     <user id="001">  
  5.         <name>王五</name>  
  6.         <email>2222222</email>  
  7.         <qq>4fenuang</qq>  
  8.         <wife>6fenuang</wife>  
  9.     </user>  
  10.   
  11.     <user id="002">  
  12.         <name>李四</name>  
  13.         <email>4444444</email>  
  14.         <qq>4fenuang</qq>  
  15.         <wife>6fenuang</wife>  
  16.     </user>  
  17.   
  18.   
  19.     <user id="003">  
  20.         <name>张三</name>  
  21.         <email>2fenuang</email>  
  22.         <qq>333333</qq>  
  23.         <wife>6fenuang</wife>  
  24.     </user>  
  25.   
  26.   
  27.   
  28. </contestList>  

转载:https://blog.csdn.net/qq_34533413/article/details/52470842

猜你喜欢

转载自blog.csdn.net/wjx_jasin/article/details/80340254