使用XPath对象解析xml文件

使用XPath对象解析xml文件

1.DocumentBuilderFactory类  工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器

其构造方法受保护,用newInstance()实例化

2.创建解析器

DocumentBuilder

使用这个类,应用程序员可以从XML获得一个Document。

这个类的实例可从DocumentBuilderFactory.newDocumentBuilder()方法获得。

一旦获得此类实例,可以从各种输入源解析XML。这些输入源是InputStreams,Files,URL和SAX InputSources。

----其构造方法是受保护的

 public static void main(String[] args) throws Exception {
// 创建解析工厂
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
// 创建解析器
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
// 通过解析器读取配置文件--返回Document对象树--org.w3c.dom.Document
Document doc = documentBuilder.parse("config/bookstore.xml"); // 创建XPath对象
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
/*
* 1.获取bookstore节点下的book属性category值为web下的第二个 title节点的文本内容
* bookstore-》book[@category='web'][2]-》title
* xpath路径:/bookstore/book[@category='web'][2]/title/text()
*/
String titleXpath = "/bookstore/book[@category='web'][2]/title/text()";
String titleVlue = (String) xPath.evaluate(titleXpath, doc, XPathConstants.STRING);
System.out.println("1:" + titleVlue);
/*
* 2.获取bookstore节点下book属性category值为web的title属性为en的节点内容
* xpath路径:/bookstore/book[@category='web']/title[@lang='en']/text() 选取属性为en
* 的title[@lang='en']
*/
String titleXpath2 = "/bookstore/book[@category='web']/title[@lang='en']/text()";
String titleValue2 = (String) xPath.evaluate(titleXpath2, doc, XPathConstants.STRING);
System.out.println("2:" + titleValue2);
/*
* 3.获取bookstore下book属性category值为cooking的title的lang属性的值
* xpath路径:/bookstore/book[@category='cooking']/title/@lang
*/
// 获取属性的值
String str1 = "/bookstore/book[@category='cooking']/title/@lang";
String shuxing = (String) xPath.evaluate(str1, doc, XPathConstants.STRING);
System.out.println("3:" + shuxing);
/*
* 4.获取bookstore节点下的所有book的节点集合 /bookstore/book
*/
// 返回值是节点集合
String str3 = "/bookstore/book";
NodeList all = (NodeList) xPath.evaluate(str3, doc, XPathConstants.NODESET);
// 开始遍历集合
for (int i = 0; i < all.getLength(); i++) {
// book节点
Element bookelt = (Element) all.item(i);// i是获取第几个
String titleValue = (String) xPath.evaluate("title", bookelt, XPathConstants.STRING);
String authorValue = (String) xPath.evaluate("author", bookelt, XPathConstants.STRING);
String yearValue = (String) xPath.evaluate("year", bookelt, XPathConstants.STRING);
String priceValue = (String) xPath.evaluate("price", bookelt, XPathConstants.STRING); System.out.println(
"title:" + titleValue + " author:" + authorValue + " year:" + yearValue + " price:" + priceValue); }
}

//config/bookstore.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
1.获取bookstore节点下的book属性category值为web下的第二个
title节点的文本内容
bookstore-》book[@category='web'][2]-》title
xpath路径:/bookstore/book[@category='web'][2]/title/text()
-->
<!--
2.获取bookstore节点下book属性category值为web的title属性为en的节点内容
xpath路径:/bookstore/book[@category='web']/title[@lang='en']
选取属性为en 的title[@lang='en']
-->
<!--
3.获取bookstore下book属性category值为cooking的title的lang属性的值
xpath路径:/bookstore/book[@category='cooking']/title/@lang
-->
<!--
4.获取bookstore节点下的所有book的节点集合
/bookstore/book
-->
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>XJS</author>
<year>2019</year>
<price>79.88</price>
</book>
<book category="web">
<title lang="uk">Learning stack</title>
<author>XJS</author>
<year>2019</year>
<price>100.00</price>
</book>
</bookstore>
上一篇:The last packet successfully received from the server was 2,926,157 milliseconds ago. The last packet sent successfully to the server was 2,926,158 milliseconds ago. is longer than the server configured value of 'wait_timeout'. 解决办法


下一篇:ios 关于UIView 的multipleTouchEnabled 和 exclusiveTouch