Java 读取xml文件内容程序小结
来源:互联网 发布:2017盒子免费视频软件 编辑:程序博客网 时间:2024/04/30 04:22
1. xml的配置<objects>
<object id="AjcbYwSaxyr">
<className>AjcbYwSaxyr</className>
<name>涉案嫌疑人</name>
<feilds pkName="id">
<feild name="AJBH" title="案件编号" displayInList="true"
displayInDetail="true"></feild>
<feild name="RYBH" title="人员编号" displayInList="true"
displayInDetail="true"></feild>
<feild name="ZZDXZQHMC" title="实际居住地行政区划名称" displayInList="true"displayInDetail="true"></feild>
<feild name="YWGXBMMC" title="业务管辖部门名称" displ
displayInDetail="true"></feild>
<feild name="case XB when '1' then '男' when '2' then '女' end" title="性别" displayInList="true"displayInDetail="true"></feild>
<feild name="CSRQ" title="出生日期" displayInList="true"
displayInDetail="true"></feild>
</feilds>
<where>
1=1
</where>
<links>
<link name="bbb">
<relate selfField="RYBH"outerField="RYBH"/>
<relate selfField="XM"outerField="XM"/>
</link>
</links>
</object>
<object id="bbb">
<className>AjcbYwSaxyr</className>
<name>涉案嫌疑人</name>
<feilds pkName="id">
<feild name="AJBH" title="案件编号" displayInList="true"
displayInDetail="true"></feild>
<feild name="RYBH" title="人员编号" displayInList="true"
displayInDetail="true"></feild>
<feild name="ZZDXZQHMC" title="实际居住地行政区划名称" displayInList="true"displayInDetail="true"></feild>
<feild name="YWGXBMMC" title="业务管辖部门名称" displayInList="true"displayInDetail="true"></feild>
<feild name="XM" title="姓名" displayInList="true"
displayInDetail="true"></feild>
<feild name="case XB when '1' then '男' when '2' then '女' end" title="性别" displayInList="true"displayInDetail="true"></feild>
<feild name="CSRQ" title="出生日期" displayInList="true"
displayInDetail="true"></feild>
</feilds>
<where>
1=1
</where>
</object>
</objects>
2.java程序
ClassPathResource cpr = new ClassPathResource(fileName);加载配置文件信息
File f = cpr.getFile();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(f);用SAX的方式读取配置文件内容,此方式的好处是一行一行的加载配置文件,不是一次把整个文件全部读取上来。
String path = "//objects/object[@id='" + 对象的id + "']";
Node node = document.selectSingleNode(path);设置读取其中一个对象的路径
String pkName = node.selectSingleNode("./feilds").valueOf("@pkName");读取对象里所有属性的集合的标示。用来循环遍历得到属性集合里的单个属性值。
List<Node> fieldNodes=node.selectNodes("./feilds/feild[@displayInList='true']");这里是读取封装的fields集合里对应的field的值,但是有限制条件必须displayInList='true'这样做的好处是我们可以对配置里的部分属性不符合条件的信息过滤掉
for(Node n:fieldNodes)
{
selectText.append("," + n.valueOf("@name"));
}
这里是得到每个符合条件的field里name属性的值;这里我只是给他拼成一个字符串,你也可以把它放到一个集合,数组里都行。
String objectNameText = node.selectStringleNode(“./className”).getText();这里是得到className对应的配置的值。
String whereText = node.selectSingleNode("./where").getText();
同理是得到where对应的配置的值。3.我们还可以通过当前对象读取到其关联对象的信息:
例如
AjcbYwSaxyr对象关联到对象bbb ,相关联的属性有: RYBH,XM。
List<Node> linkNodes=node.selectNodes("./links/link");获取关联对象里的所有对象信息
for(Node n:linkNodes)
{
String linkObjectId = n.valueOf("@name");循环输出所有关联对象id值。
Node linkObject = document.selectSingleNode("//objects/object[@id='" + linkObjectId + "']");得到关联对象的读取路径
4.记得引入两个工具包
jaxen-1.1.1.jar;log4j-1.2.14.jar
- Java 读取xml文件内容程序小结
- java操作XML文件--读取内容
- Java读取XML文件的内容
- java读取XML文件内容的方法
- 读取xml文件内容
- 读取XML文件内容
- java程序读取文件内容打印控制台
- ue4 Xml文件读取小结
- 如何读取XML文件内容!
- 如何读取XML文件内容
- java读取文件夹里的所有xml文件内容
- Java中DOM读取xml文件中的内容
- java 读取XML文件里的节点内容
- Java读取文件内容
- JAVA读取文件内容
- Java读取文件内容
- java读取文件内容
- java文件内容读取
- Oracle常用函数整理
- Fire Net
- 如何查看数据库的空间使用情况
- UltraEdit 15.10 注册机
- 把虚拟系统接入网络--VMware网络设置
- Java 读取xml文件内容程序小结
- doubanclaim4c142e78270ee279
- winhtmleditor 控件(CS模式下的HTML编辑软件)
- return 和每个人都比前一个大5岁
- 传智播客Spring视频教程学习笔记11
- 32位程序如何访问64位的注册表
- 终于找到可以在win32 sdk下打开文件选择对话框的方法。
- c#中virtural, new, override
- php提示错误:Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO)