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"));

           }

这里是得到每个符合条件的fieldname属性的值;这里我只是给他拼成一个字符串,你也可以把它放到一个集合,数组里都行。

String objectNameText = node.selectStringleNode(“./className”).getText();这里是得到className对应的配置的值。
String whereText = node.selectSingleNode("./where").getText();
同理是得到where对应的配置的值。3.我们还可以通过当前对象读取到其关联对象的信息:
例如
AjcbYwSaxyr对象关联到对象bbb     ,相关联的属性有: RYBHXM
 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.jarlog4j-1.2.14.jar