STEP1: 大致了解xml文件内容布局
1) 文件开头一句<?xml version="1.0" encoding="UTF-8"?>
这句话的作用:明确xml版本和编码;版本一般默认为1.0,不做过多理解;编码解决了中文乱码问题,使得该xml文档在保存时即为UTF-8编码方式保存。
2) 开启根标签,整个文档有且只有一个根标签,其余的内容均在根标签中添加
例如:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"
default-init-method="init">
………………
</beans>
以上例子<beans>即为某一文档的根标签,省略号部分为其中的具体内容;在根标签的开头中,我们可以看到很多的xmlns为开头的内容。这部分为该文档的约束内容,这些约束内容的作用是规范文档中元素的编写规则。以上例子的约束为Schema约束,此外还有一种约束为DTD约束,约束文档引入方式不同于Schema约束。关于约束文档,可以另外寻找相关内容去了解其语法规则。
3) 子元素理解
例如1:schema约束下的元素解读
<aop:aspectj-autoproxy proxy-target-class="true" />:根标签中的aop命名空间下的aspectj-autoproxy元素的属性proxy-target-class值为true
例如2:schema约束下的元素解读(单一名称空间)
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
当在schema的约束下,只有一个名称空间时,该名称空间可省略,即上述例子中红色中省略了web-app:,直接写元素名称welcome-file-list
例如3:DTD约束下的元素解读
<select id="get" resultType="com.united.module.business.model.SysSupply"> content </select>
select为标签,id/resultType为标签属性,content为标签内容
STEP2: XML解析技术
xml解析方法为: DOM解析(非官方,需要导jar包),SAX解析(官方);
DOM解析是通过解析工具dom4j,将文档读取为document树,文档中的标签元素、属性、文件均解析为一个节点Node对象,分别对应为标签节点(Element),属性节点(Attribute),文本节点(Text);
相关方法语句如下:
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("./src/contact.xml"));
节点: Iterator Element.nodeIterator(); //获取当前标签节点下的所有子节点
标签: Element Document.getRootElement(); //获取xml文档的根标签
Element ELement.element("标签名") //指定名称的第一个子标签
Iterator<Element> Element.elementIterator("标签名");// 指定名称的所有子标签
List<Element> Element.elements(); //获取所有子标签
属性:String Element.attributeValue("属性名") //获取指定名称的属性值
Attribute Element.attribute("属性名");//获取指定名称的属性对象
Attribute.getName() //获取属性名称
Attibute.getValue() //获取属性值
List<Attribute> Element.attributes(); //获取所有属性对象
Iterator<Attribute> Element.attibuteIterator(); //获取所有属性对象
文本: Element.getText(); //获取当前标签的文本
Element.elementText("标签名") //获取当前标签的指定名称的子标签的文本内容
STEP3: xPath技术
在DOM解析的过程中,如果文档的标签层次较多,那么需要获取较为底层的标签元素、属性、文本时,需要层层深入,步骤较为繁琐,故此处引入xPath技术,简化其检索过程。
1)导入xPath支持jar包 。 jaxen-1.1-beta-6.jar
2)使用xpath方法
List<Node> selectNodes("xpath表达式"); 查询多个节点对象
Node selectSingleNode("xpath表达式"); 查询一个节点对象
3) xPath表达式语法
/ 绝对路径 表示从xml的根位置开始或子元素(一个层次结构)
// 相对路径 表示不分任何层次结构的选择元素。
* 通配符 表示匹配所有元素
[] 条件 表示选择什么条件下的元素
@ 属性 表示选择属性节点
and 关系 表示条件的与关系(等价于&&)
text() 文本 表示选择文本内容