使用dom4j来操作,直接贴代码
1 package qiaokai.util; 2 3 import java.io.File; 4 import java.util.LinkedList; 5 import java.util.List; 6 7 import org.dom4j.Document; 8 import org.dom4j.DocumentException; 9 import org.dom4j.Element; 10 import org.dom4j.io.SAXReader; 11 12 public class XMLParser { 13 public static final String DEFAULT_PATH = "config.xml"; 14 private LinkedList<KV> KVList; 15 private String filePath; 16 private String filterNode; 17 18 private XMLParser(Builder b){ 19 this.filePath = b.path; 20 this.filterNode = b.filterNode; 21 KVList = new LinkedList<>(); 22 readConfig(filePath); 23 } 24 25 /** 得到文件的路径 */ 26 public String getFilePath(){ 27 return filePath; 28 } 29 /** 得到设置的过滤节点名字 */ 30 public String getFilterNode(){ 31 return filterNode; 32 } 33 34 /** 得到 key-value 的链表 */ 35 public LinkedList<KV> getKVList(){ 36 return KVList; 37 } 38 39 /** 读取xml文件,并且把内容保存到List 链表中 */ 40 private void readConfig(String filePath) { 41 if(QiaoUtils.isStrBlack(filePath)){ 42 QiaoUtils.Log("XML文件的设置路径为空"); 43 return; 44 } 45 File f = new File(filePath); 46 if(!f.exists()){ 47 QiaoUtils.Log(filePath+" 文件不存在,请检查设置是否正确"); 48 return; 49 } 50 try { 51 Document doc = new SAXReader().read(f); 52 Element ele = doc.getRootElement(); 53 filterNode(ele, KVList,filterNode); 54 } catch (DocumentException e) { 55 e.printStackTrace(); 56 } 57 } 58 /** 59 * 设置过滤,这样可以只得到过滤后的list KV链表 60 * @param str 61 */ 62 @SuppressWarnings("unchecked") 63 private void filterNode(Element ele,final LinkedList<KV> KVlist,String str){ 64 if(ele.getName().equals(str)){ 65 traverNode(ele,KVlist); 66 return; 67 } 68 if(ele.nodeCount() > 1){ 69 List<Element> el = ele.elements(); 70 for(Element e : el){ 71 filterNode(e, KVlist, str); 72 } 73 } 74 } 75 /** 递归遍历xml */ 76 @SuppressWarnings("unchecked") 77 private void traverNode(Element ele,final LinkedList<KV> KVlist){ 78 if(ele.nodeCount()>1){ //其子节点个数大于0 79 List<Element> el = ele.elements(); 80 for(Element e : el){ 81 traverNode(e,KVlist); 82 } 83 } else { 84 String key = ele.getName(); 85 String value = ele.getText(); 86 QiaoUtils.Log("K: "+key+" V:"+value); 87 KVlist.addLast(new KV(key,value)); 88 } 89 } 90 91 /** 使用建造者模式来构造 */ 92 public static class Builder{ 93 String path; 94 String filterNode; 95 public Builder setFilePath(String filePath) { 96 path = filePath; 97 return this; 98 } 99 /** 设置过滤,这样可以在一个config.xml文件里读取很多个Java类需要的配置信息 */ 100 public Builder setfilterNode(String node) { 101 filterNode = node; 102 return this; 103 } 104 public XMLParser build() throws Exception{ 105 if(QiaoUtils.isStrBlack(path) || QiaoUtils.isStrBlack(filterNode)){ 106 QiaoUtils.Log("检查config.xml的文件路径设置或者需要过滤的Node节点名字"); 107 throw new Exception(); 108 } 109 return new XMLParser(this); 110 } 111 } 112 113 public class KV{ 114 private String key; 115 private String value; 116 public KV(String key, String value){ 117 this.key = key; 118 this.value = value; 119 } 120 public String getKey() { 121 return key; 122 } 123 public void setKey(String key) { 124 this.key = key; 125 } 126 public String getValue() { 127 return value; 128 } 129 public void setValue(String value) { 130 this.value = value; 131 } 132 133 } 134 }
其中xml文件的格式可以类似这样
1 <?xml version="1.0" encoding="utf-8"?> 2 <nywebsite> 3 <!-- 这个是PostMail类中需要配置的信息 --> 4 <postmail> 5 <email>9527@qq.com</email> 6 <password>这里输入密码</password> 7 <hostname>smtp.qq.com</hostname> 8 </postmail> 9 <mrnjvideo> 10 <keywords> 11 <word>毛乌素</word> 12 </keywords> 13 </mrnjvideo> 14 </nywebsite>
使用XMLPraser类的时候,可以这样
1 package qiaokai.test; 2 3 import java.util.ArrayList; 4 import java.util.LinkedList; 5 6 import qiaokai.main.PostEmail; 7 import qiaokai.util.XMLParser; 8 import qiaokai.util.XMLParser.KV; 9 10 public class TestXMLParser { 11 12 public static void main(String[] args) throws Exception { 13 XMLParser xp = new XMLParser.Builder() 14 .setFilePath(XMLParser.DEFAULT_PATH) 15 .setfilterNode(PostEmail.FILTER_NODE) 16 .build(); 17 LinkedList<KV> KVList = xp.getKVList(); 18 ArrayList<String> keys = new ArrayList<>(); 19 for(KV kv : KVList){ 20 if(kv.getKey().equals("email")){ 21 String address = kv.getValue(); 22 } else if (kv.getKey().equals("password")){ 23 String password = kv.getValue(); 24 } else if (kv.getKey().equals("hostname")){ 25 String hostname = kv.getValue(); 26 } 27 } 28 } 29 }