XMLConfiguration用法
来源:互联网 发布:激战2捏脸数据 编辑:程序博客网 时间:2024/04/25 00:04
XMLConfiguration是apache工具集里的一个用于对XML文件进行读写的东东,使用起来很方便。下面我将简单的举几个使用例子。
1,读
这里是我们的XML文件:unit.xml
<?xml version="1.0" encoding="utf-8"?>
<tree>
<unit id="root" type="root">
<back-ground>/root/Desktop/image/4.jpg</back-ground>
<unit id="1" type="alarm">
<back-ground>/root/Desktop/image/5.jpg</back-ground>
<father>root</father>
<position>x=250:y=225</position>
</unit>
</unit>
</tree>
下面是java代码:
//创建XMLConfiguration
private static XMLConfiguration getConfiguration(File file) {
if (file == null || !file.exists())
return null;
XMLConfiguration config = null;
try {
config = new XMLConfiguration(file);
} catch (ConfigurationException e) {
e.printStackTrace();
}
config.setEncoding("utf-8");//设置编码
return config;
}
.........
XMLConfiguration tool = getConfiguration("/home/dx/unit.xml");
//获取节点值
String rootBackGround = tool.getString("unit.back-ground");//这里获取的值是/root/Desktop/image/4.jpg
String alarmBackGround = tool.getString("unit.unit.back-ground");//这里获取的值是/root/Desktop/image/5.jpg
//获取节点属性
String rootID = tool.getString("unit[@id]");//这里获取的值是root
String alarmID = tool.getString("unit.unit[@id]");//这里获取的值是1
//获取重复节点值
如果有两个以上并级的节点如何区分呢?比如在第一级unit下有两个并列的unit:
......................................
<unit id="root" type="root">
<back-ground>/root/Desktop/image/4.jpg</back-ground>
<unit id="1" type="alarm">
<back-ground>/root/Desktop/image/5.jpg</back-ground>
<father>root</father>
<position>x=250:y=225</position>
</unit>
<unit id="2" type="alarm">
<back-ground>/root/Desktop/image/6.jpg</back-ground>
<father>root</father>
<position>x=300:y=270</position>
</unit>
</unit>
...............................
String firstID = tool.getString("unit.unit(0)[@id]");//这里获取的值是1
String secondID = tool.getString("unit.unit(1)[@id]");//这里获取的值是2
写操作基本基本更读的代码差不多(请先看上一篇:XMLConfiguration使用手记(1 读)) 只是这里我们用addProperty或setProperty
config.setProperty("unit.unit(0)[@id]", "0");
config.setProperty("unit.unit(1)[@id]", "1");
当然还是更读一样 要首先有个xml文件(空的也可以 但必须有根节点,其他节点会自动生成,当然也可以修改指定节点,方法还是一样的)。
这里要注意 XMLConfiguration默认情况下以','为分割符如果一个节点值里出现逗号 他会把他分开成两个节点例如:
config.setProperty("unit.unit", "a,b");
我们想要的结果是
...............................
<unit>
a,b
</unit>
.....................
可结果会变成
...............................
<unit>
a
</unit>
<unit>
b
</unit>
.....................
如果想要使用逗号 可以设置XMLConfiguration的delimiter例如
XMLConfiguration.setDelimiter(' ');//设置空格为分割符.
别望了写完save一下.
config.save();
- XMLConfiguration用法
- XMLConfiguration 的使用
- XMLConfiguration读取XML文件
- 使用 POCO 中的 XMLConfiguration 管理配置文件
- JAVA XMLConfiguration解决StringXML及多节点
- XMLConfiguration中不能使用英文逗号的解决办法
- XMLConfiguration中不能使用英文逗号的解决办法
- org.apache.commons.configuration.XMLConfiguration 可以用来读取和写入 xml 文件
- Servlet使用XMLConfiguration解析在线XML(或本地文件系统中的XML文件)
- java的一些实用工具类(XMLConfiguration/MapUtils/JSON/JSONArray等)
- 好记性不如烂笔头60-利用XMLConfiguration解析XML多节点
- 用法
- ,, 用法
- 用法
- 用法
- #用法
- ??用法
- !!用法
- CentOS下后台运行Python脚本及关闭脚本的一些操作
- 一,Log4j Example
- 第一章 PL/SQL介绍
- DNS 引起经典RAC故障
- 汉字转拼音
- XMLConfiguration用法
- Android中基于Socket的网络通信
- 判断网络
- UITableView从storyboard中加入cell
- 很多时候,很多程序员都是这样的......
- MySQL为表增加一列属性解决方案
- springmvc中的interceptor配置
- MySQL 使用 SSL 连接(附 Docker 例子)
- 嵌入式Linux字符设备驱动模型详解