java读取XML

xml源文件如下:

Java代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <memcachesversion="1">
  3. <memcachetype="AAAA">
  4. <ip>11.11.1.244:11211</ip>
  5. <prefix>aaaa_</prefix>
  6. </memcache>
  7. <memcachetype="BBBB">
  8. <ip>11.22.1.243:11211</ip>
  9. <prefix>bbbb_</prefix>
  10. </memcache>
  11. <memcachetype="CCCC">
  12. <ip>11.33.33.152:11211</ip>
  13. <prefix>cccc_</prefix>
  14. </memcache>
  15. <memcachetype="DDDD">
  16. <ip>11.44.15.244:11211</ip>
  17. <prefix>dddd_</prefix>
  18. </memcache>
  19. <memcachetype="EEEEEEE">
  20. <ip>11.66.16.152:11211</ip>
  21. <prefix>eeeee_</prefix>
  22. </memcache>
  23. <memcachetype="FFFFF">
  24. <ip>11.77.170.152:11211</ip>
  25. <prefix>fffff_</prefix>
  26. </memcache>
  27. </memcaches>
<?xml version="1.0" encoding="UTF-8"?> <memcaches version="1"> <memcache type="AAAA"> <ip>11.11.1.244:11211</ip> <prefix>aaaa_</prefix> </memcache> <memcache type="BBBB"> <ip>11.22.1.243:11211</ip> <prefix>bbbb_</prefix> </memcache> <memcache type="CCCC"> <ip>11.33.33.152:11211</ip> <prefix>cccc_</prefix> </memcache> <memcache type="DDDD"> <ip>11.44.15.244:11211</ip> <prefix>dddd_</prefix> </memcache> <memcache type="EEEEEEE"> <ip>11.66.16.152:11211</ip> <prefix>eeeee_</prefix> </memcache> <memcache type="FFFFF"> <ip>11.77.170.152:11211</ip> <prefix>fffff_</prefix> </memcache> </memcaches>



解析上面xml文件的java代码:

Java代码
  1. importjavax.xml.parsers.DocumentBuilder;
  2. importjavax.xml.parsers.DocumentBuilderFactory;
  3. importjava.io.*;//Java基础包,包含各种IO操作
  4. importjava.util.*;//Java基础包,包含各种标准数据结构操作
  5. importjavax.xml.parsers.*;//XML解析器接口
  6. importorg.w3c.dom.*;
  7. importcom.happyelements.apollo.config.type.AppType;
  8. importcom.happyelements.apollo.util.ApolloCollectionsUtil;
  9. publicclassParseMemXML{
  10. publicstaticfinalMap<String,MemBean>memMap=ApolloCollectionsUtil.newMap();//将MemBean放到map中存放
  11. //解析xml文件并将得到的实例放入map中
  12. publicstaticvoidinitXMLFile(Stringfilename)throwsException{
  13. //为解析xml做准备
  14. //创建DocumentBuilderFactory实例,指定DocumentBuilder
  15. DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
  16. DocumentBuilderdb=null;
  17. try{
  18. db=dbf.newDocumentBuilder();
  19. }catch(ParserConfigurationExceptionpce){
  20. System.out.println(pce);
  21. //出异常时输出异常信息,然后退出,下同
  22. System.exit(1);
  23. }
  24. Documentdoc=null;
  25. try{
  26. doc=db.parse(filename);
  27. }catch(DOMExceptiondom){
  28. System.err.println(dom.getMessage());
  29. System.exit(1);
  30. }catch(IOExceptionioe){
  31. System.err.println(ioe);
  32. System.exit(1);
  33. }
  34. //下面是解析XML的全过程,
  35. //比较简单,先取根元素"memcaches"
  36. Elementroot=doc.getDocumentElement();
  37. //取"memcache"元素列表
  38. NodeListmemcaches=root.getElementsByTagName_r("memcache");
  39. for(inti=0;i<memcaches.getLength();i++){
  40. //创建一个memcache的Bean实例
  41. MemBeanmemBean=newMemBean();
  42. //依次取每个"memcache"元素
  43. Elementmemcache=(Element)memcaches.item(i);
  44. //取memcache的type属性
  45. memBean.setType(memcache.getAttribute("type"));
  46. //取"ip"元素,下面类同
  47. NodeListips=memcache.getElementsByTagName_r("ip");
  48. if(ips.getLength()==1){
  49. Elemente=(Element)ips.item(0);
  50. Textt=(Text)e.getFirstChild();
  51. memBean.setIp(t.getNodeValue());
  52. System.out.println(memBean.getIp());
  53. }
  54. NodeListprefixs=memcache.getElementsByTagName_r("prefix");
  55. if(prefixs.getLength()==1){
  56. Elemente=(Element)prefixs.item(0);
  57. Textt=(Text)e.getFirstChild();
  58. memBean.setPrefix(t.getNodeValue());
  59. System.out.println(memBean.getPrefix());
  60. }
  61. memMap.put(memBean.getType().toString(),memBean);
  62. }
  63. }
  64. publicstaticvoidmain(String[]args)throwsException{
  65. ParseMemXML.initXMLFile("E:\\workplace\\Tool2\\WebRoot\\WEB-INF\\memconf.xml");
  66. System.out.println(ParseMemXML.memMap.size());
  67. MemBeanmemBean=ParseMemXML.memMap.get(AppType.AAAA.toString());
  68. System.out.println("ip====="+memBean.getIp());
  69. System.out.println("prefix==="+memBean.getPrefix());
  70. }
  71. }
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.*; //Java基础包,包含各种IO操作 import java.util.*; //Java基础包,包含各种标准数据结构操作 import javax.xml.parsers.*; //XML解析器接口 import org.w3c.dom.*; import com.happyelements.apollo.config.type.AppType; import com.happyelements.apollo.util.ApolloCollectionsUtil; public class ParseMemXML { public static final Map<String, MemBean> memMap = ApolloCollectionsUtil.newMap();//将MemBean放到map中存放 //解析xml文件并将得到的实例放入map中 public static void initXMLFile(String filename) throws Exception { // 为解析xml做准备 // 创建DocumentBuilderFactory实例,指定DocumentBuilder DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.out.println(pce); // 出异常时输出异常信息,然后退出,下同 System.exit(1); } Document doc = null; try { doc = db.parse(filename); } catch (DOMException dom) { System.err.println(dom.getMessage()); System.exit(1); } catch (IOException ioe) { System.err.println(ioe); System.exit(1); } // 下面是解析XML的全过程, // 比较简单,先取根元素"memcaches" Element root = doc.getDocumentElement(); // 取"memcache"元素列表 NodeList memcaches = root.getElementsByTagName_r("memcache"); for (int i = 0; i < memcaches.getLength(); i++) { // 创建一个memcache的Bean实例 MemBean memBean = new MemBean(); // 依次取每个"memcache"元素 Element memcache = (Element) memcaches.item(i); // 取memcache的type属性 memBean.setType(memcache.getAttribute("type")); // 取"ip"元素,下面类同 NodeList ips = memcache.getElementsByTagName_r("ip"); if (ips.getLength() == 1) { Element e = (Element) ips.item(0); Text t = (Text) e.getFirstChild(); memBean.setIp(t.getNodeValue()); System.out.println(memBean.getIp()); } NodeList prefixs = memcache.getElementsByTagName_r("prefix"); if (prefixs.getLength() == 1) { Element e = (Element) prefixs.item(0); Text t = (Text) e.getFirstChild(); memBean.setPrefix(t.getNodeValue()); System.out.println(memBean.getPrefix()); } memMap.put(memBean.getType().toString(), memBean); } } public static void main(String[] args) throws Exception { ParseMemXML.initXMLFile("E:\\workplace\\Tool2\\WebRoot\\WEB-INF\\memconf.xml"); System.out.println(ParseMemXML.memMap.size()); MemBean memBean = ParseMemXML.memMap.get(AppType.AAAA.toString()); System.out.println("ip=====" + memBean.getIp()); System.out.println("prefix===" + memBean.getPrefix()); } }



MemBean的java源文件

Java代码
  1. importjava.io.Serializable;
  2. importorg.simpleframework.xml.Attribute;
  3. importorg.simpleframework.xml.Element;
  4. importorg.simpleframework.xml.Root;
  5. @Root(name="memconf")
  6. publicclassMemBeanimplementsSerializable{
  7. privatestaticfinallongserialVersionUID=2209681159359089305L;
  8. @Attribute
  9. privateStringtype;
  10. @Element
  11. privateStringip;
  12. @Element
  13. privateStringprefix;
  14. publicStringgetType(){
  15. returntype;
  16. }
  17. publicvoidsetType(Stringtype){
  18. this.type=type;
  19. }
  20. publicStringgetIp(){
  21. returnip;
  22. }
  23. publicvoidsetIp(Stringip){
  24. this.ip=ip;
  25. }
  26. publicStringgetPrefix(){
  27. returnprefix;
  28. }
  29. publicvoidsetPrefix(Stringprefix){
  30. this.prefix=prefix;
  31. }
  32. }
import java.io.Serializable; import org.simpleframework.xml.Attribute; import org.simpleframework.xml.Element; import org.simpleframework.xml.Root; @Root(name = "memconf") public class MemBean implements Serializable { private static final long serialVersionUID = 2209681159359089305L; @Attribute private String type; @Element private String ip; @Element private String prefix; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } }




AppType源文件

Java代码
  1. publicenumAppType{
  2. AAAA,
  3. BBBB,
  4. CCCC,
  5. DDDD,
  6. EEEE,
  7. FFFF,
  8. ;
  9. }

你可能感兴趣的:(java,数据结构,bean,xml,Web)