博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
XML的解析
阅读量:7071 次
发布时间:2019-06-28

本文共 3360 字,大约阅读时间需要 11 分钟。

XML的解析

  1. java中配置文件的三种配置位置及读取方式

       1.1  XML*.properties(属性文件)

         1*.properties文件

             Key=value

             #注释

           //util包下的属性文件类

          Properties    properties=new Properties();

           //加载一个properties

           Properties.load(is);

           //某一个键的值

        Properties.getProperty(“键名”);

 代码如下:

//获取同包下的资源文件,将其转化为流对象      InputStream  in=PropertiesDemo.class.getResourceAsStream("/db.properties");     //文件解析工具类(Properties)       Properties  p=new Properties();     //加载资源文件          p.load(in);      //得到文件key对应的value值      System.out.println(p.getProperty("uname"));      System.out.println(p.getProperty("url"));

 

结果:

mybatis_ssmjdbc:mysql://localhost:3306/mybatis_ssm

  

 

           1.2 存放位置

            1.2.1   src根目录下

            类名:class.getresourceAsStream(“/config.properties”);

             1.2.2   与读取配置文件的类在同一包

                类名:class.getResourceAsStream(“config2properties.”);

            1.2.3 WEB-INF(或其子目录下)

                 ServletContext sc=this.getServletContext();

       InputStreais=sc.getResourceAsStream("/WEB-INF/db.properties");

     代码:

ServletContext context=request.getServletContext();         InputStream in=context.getResourceAsStream("/WEB-INF/db.properties");        //专门的工具类           Properties  p=new Properties();           p.load(in);           System.out.println(p.getProperty("uname"));           System.out.println(p.getProperty("url"));

 

 

 

 

 

 

           2 dom4j+xpath 解析xml 文件

               Xpath 等同于数据库的select语句

 

             document.selectNodes(xpath);//查一组

             document.selectSingleNode(xpath);//查单个

 代码:

           

SAXReader sax=new SAXReader();      Document dos=sax.read(new  File("D:\\Exxp\\javaEE06\\src\\com\\zking\\config.xml"));           //查一组       List   os=dos.selectNodes("/config/action");         for(Object o:os) {           Element elm=(Element)os;            System.out.print(elm.attributeValue("name"));          System.out.print(elm.attributeValue("path"));          System.out.println(elm.attributeValue("redirect"));       }       //查询一个        Element et=(Element)dos.selectSingleNode("/config/action");          System.out.println(et.attributeValue("path"));

 

 

                  DOM由节点组成

             Node

              元素节点

             属性节点

               文本节点

 

获取指定的属性节点

 

InputStream is = XmDemo.class.getResourceAsStream("students.xml");		SAXReader saxx = new SAXReader();		Document docc = saxx.read(is);      Element node=(Element)                                 docc.selectSingleNode("students/student[@sid='s002']");	    System.out.println(node.asXML());

 

  

结果:

小芳

  

 

              3 dom4J 解析

              SAXReader                       解析器

            Read(XML路径)                得到document对象

             getRootElement               获取根节点

               Element(节点名称)             获取根节点下的指定节点信息

              attributeValue(属性名)        获取属性值

            Element(节点名称)              获取节点的Element对象

             getText()                     获取节点的值

作业

 

// Xpath解析 能够将xml格式的串当做目录结构来进行查找		List
selectNodes = doc.selectNodes("config.xml"); for(Object o:doc.selectNodes("/config/action")) { Element elm=(Element)o; // 1、获取所有action中的type的值 System.out.println(elm.attributeValue("type")); } // 2、获取第二个action中的type的值 Element node=(Element) doc.selectSingleNode("/config/action[@path='/loginAction']"); System.out.println(node.attributeValue("type"));// // 3、获取第二个action的所有forward的path for(Object o:doc.selectNodes("/config/action/forward")) { Element elm=(Element)o; // System.out.println(elm.attributeValue("path")); } // 4、获取第二个action的第二个forward的path Element nodee=(Element) doc.selectSingleNode("/config/action/forward[@class='sa']"); // System.out.println(nodee.attributeValue("path"));

 

  

 

转载于:https://www.cnblogs.com/xmf3628/p/10917240.html

你可能感兴趣的文章
checkboxlist 横向显示,自动换行
查看>>
MQ:Introducing Advanced Messaging
查看>>
[转]oracle 同义词 synonym
查看>>
Java List 生成 树
查看>>
使用ASP.Net WebAPI构建REST服务(五)——客户端
查看>>
VC中利用多线程技术实现线程之间的通信
查看>>
Mod_Python中文文档
查看>>
ASP.NET MVC学习之视图篇(1)
查看>>
OAF_OAF Framework常用函数汇总(概念)
查看>>
用开源Look&Feel (Substance)写 漂亮的Swing应用程序
查看>>
和借钱有关(五)
查看>>
Android开发 MMS支持 创建和编辑MMS
查看>>
.NET常用的扩展方法整理
查看>>
jquery 插件开发及extend
查看>>
“大型票务系统”和“实物电商系统”在接入管理方面的差异
查看>>
PHP 打印调用函数入口地址(堆栈),方便调式
查看>>
TIMESTEN安装配置指南-中文版
查看>>
菜鸟学SSH(十四)——Spring容器AOP的实现原理——动态代理
查看>>
Codeforces Round #256 (Div. 2) B Suffix Structures
查看>>
我的Android开发相关文章
查看>>