首先,贴上自己的实例:

XML文件:NewFile.xml(该文件与src目录同级)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property>
<name>s3Bucket</name>
<value></value>
<description>get s3Bucket to get data</description>
</property>
<property>
<name>s3key</name>
<value>2016-12-19/12:26:36</value>
<description></description>
</property>
<property>
<name>DynamoDBTable</name>
<value>longyauntest</value>
<description></description>
</property>
<property>
<name>KINESIS_STREAM_NAME</name>
<value></value>
<description></description>
</property>
<property>
<name>Region</name>
<value>cn-north-1</value>
<description></description>
</property>
<property>
<name>LogFilePath</name>
<value></value>
<description>save logfile to somewhere in s3
eg:s3bucket://prefix key.</description>
</property>
</configuration>

读取类:

String CONFIGXML_FILEPATH="NewFile.xml";
Map<String, String> propertyMap = new HashMap<String, String>();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(CONFIGXML_FILEPATH);
// 获取根元素
Element configuration = doc.getDocumentElement(); // System.out.println(rootElement); // 获取根元素下面的子节点列表
NodeList propertyList = configuration.getChildNodes(); for (int i = 0; i < propertyList.getLength(); i++) {
// 获取每个子节点
Node property = propertyList.item(i); String propertyName = null;
String propertyValue = null; //该list包括单个property中的各个子节点,包括name、value、description
NodeList nodeList = property.getChildNodes();
// 遍历该节点的详细信息
for (int j = 0; j < nodeList.getLength(); j++) {
Node propertyDetail = nodeList.item(j); if (!propertyDetail.getNodeName().equals("#text")) {
// 获取属性名
if (propertyDetail.getNodeName().equals("name")) {
propertyName = propertyDetail.getTextContent();
}
// 获取属性值
if (propertyDetail.getNodeName().equals("value")) {
propertyValue = propertyDetail.getTextContent();
}
}
}
// 如果属性值不为null,则将属性放入map中
if (propertyValue != null) {
PropertyMap.put(propertyName, propertyValue);
System.out.println(propertyName + ":" + propertyValue);
}
}

         //打印
// for(String key:property.keySet())
// {
// System.out.println(key+":"+property.get(key));
// } } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

ok,以下是别人的:

1.java读取xml文件的四种方法

2.四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)(这个妹纸比较6)

3.java解析xml文件(三种方式-(dom ,jdom ,dom4j)

java 读取XML文件作为配置文件的更多相关文章

  1. JAVA读取XML文件并解析获取元素、属性值、子元素信息

    JAVA读取XML文件并解析获取元素.属性值.子元素信息 关键字 XML读取  InputStream   DocumentBuilderFactory   Element     Node 前言 最 ...

  2. java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .

    背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...

  3. java读取XML文件的四种方式

    java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...

  4. java读取 xml文件

    java读取xml文件的四种方法  转自https://www.cnblogs.com/lingyao/p/5708929.html Xml代码 1 <?xml version="1. ...

  5. JAVA读取XML文件数据

    XML文档内容如下: <?xml version="1.0" encoding="UTF-8"?> <root> <field t ...

  6. java读取XML文件,及封装XML字符串

    package com.yyl.text; import java.io.FileInputStream; import java.util.ArrayList; import org.junit.T ...

  7. 通过Java读取xml文件内容

    读取XML中的内容就需要对XML进行解析,目前对XML进行解析的方法分为四种: 下面解析的方法是DOM4J,需要下载jar包dom4j:https://dom4j.github.io/ package ...

  8. java读取xml文件并转换成对象,并进行修改

    1.首先要写工具类,处理读取和写入xml文件使用的工具.XMLUtil.javaimport java.io.FileInputStream; import java.io.FileWriter; i ...

  9. 精----Java读取xml文件的四种方法

    xml文件: Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VA ...

随机推荐

  1. 实战SQL Server 2005镜像配置全过程

    SQL Server 2005镜像配置基本概念 我理解的SQL Server 2005镜像配置实际上就是由三个服务器(也可以是同一服务器的三个 SQL 实例)组成的一个保证数据的环境,分别是:主服务器 ...

  2. iOS各种动画效果

    ios各种动画效果 最普通动画: //开始动画 [UIView beginAnimations:nil context:nil];  //设定动画持续时间 [UIView setAnimationDu ...

  3. OL/SQL编程练习

    create or replace procedure pr_first is --一个变量 v_a ) := '总有一天我的生命将走到尽头'; --一个常量 c_b constant ) := '而 ...

  4. Hibernate中的一对多与多对一映射

    1.需求 一个部门有多个员工;         [一对多] 多个员工,属于一个部门    [多对一] 2.实体Bean设计 Dept: public class Dept { private int ...

  5. Cisco IOS Security command Guide

    copy system:running-config nvram:startup-config : to save your configuration changes to the startup ...

  6. error: unknown field 'ioctl' specified in initializer

    error message: 原因: 从2.6.36开始,file_operations结构发生了重大变化 具体看  xx../include/linux/fs.h定义: 取消了原先的 int (*i ...

  7. SrcollView分页加载数据(布局)

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools=& ...

  8. linux exec用法总结

    Linux中exec的用法总结 先总结一个表: exec命令 作用 exec ls 在shell中执行ls,ls结果显示结束后不返回原来的的目录中,而是/(根目录) exec <file 将fi ...

  9. C/C++中函数参数传递详解(一)

    *在定义时使用代表指针类型,其他情况代表取内容.&在定义时使用代表引用(别名),在其他情况代表取地址 在编写个人函数的时候,你将会受到C++中的一条基本的原则的限制:在默认的情况下,变量只能以 ...

  10. SharePoint 2013 搜索体系结构

    博客地址:http://blog.csdn.net/FoxDave 本文参考自微软官方的Chart,记录一下,算是自己对这部分知识的总结. Microsoft® SharePoint® Server ...