读取xml文件读取xml文件
xml源文件如下:
Java代码
,
,
,
, 11.11.1.244:11211
, aaaa_ ,
,
, 11.22.1.243:11211
? bbbb_ ,,
,,
,, 11.33.33.152:11211
,, cccc_ ,,
,,
,, 11.44.15.244:11211
,, dddd_ ,,
,?
,, 11.66.16.152:11211
,, eeeee_ ,,
,,
,, 11.77.170.152:11211 ...
读取xml文件
xml源文件如下:
Java代码
,
,
,
, 11.11.1.244:11211
, aaaa_ ,
,
, 11.22.1.243:11211
? bbbb_ ,,
,,
,, 11.33.33.152:11211
,, cccc_ ,,
,,
,, 11.44.15.244:11211
,, dddd_ ,,
,?
,, 11.66.16.152:11211
,, eeeee_ ,,
,,
,, 11.77.170.152:11211
,, fffff_ ,,
,,
复制打印
,,
,?
,, ,, 11.11.1.244:11211
,, aaaa_ ,,
,, ,, 11.22.1.243:11211
,, bbbb_ ,,
,, ,? 11.33.33.152:11211
,, cccc_ ,,
,, ,, 11.44.15.244:11211
,, dddd_ ,,
,, ,, 11.66.16.152:11211
,, eeeee_ ,?
,, ,, 11.77.170.152:11211
,, fffff_ ,,
,,
解析上面xml文件的java代码:
Java代码
,,
,, 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.A
ype; ,, import com.happyelements.apollo.util.ApolloCollectionsUtil;
,,
,, public class ParseMemXML {
,,
,, public static final Map
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("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("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("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());
,,,
,,, }
,,, }
复制打印
,,, 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 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("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("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("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代码
,,, 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; ,,, }
,,,
,,, }
复制打印
,,, 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代码
,?,
,?? public enum AppType {
,,, AAAA, ,,, BBBB, ,,, CCCC, ,,, DDDD, ,,, EEEE, ,,, FFFF, ,,,
,,, ;
,,, }
复制打印
,,? public enum AppType {
,,, AAAA, ,,, BBBB, ,,, CCCC, ,,, DDDD, ,,, EEEE, ,,, FFFF, ,,,
,,, ;
,,, }
本文档为【读取xml文件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。