使用XPath对象解析xml文件
1.DocumentBuilderFactory类 工厂API,使应用程序能从XML文档获取生成DOM对象树的解析器
其构造方法受保护,用newInstance()实例化
2.创建解析器
DocumentBuilder
使用这个类,应用程序员可以从XML获得一个Document。
这个类的实例可从DocumentBuilderFactory.newDocumentBuilder()方法获得。
一旦获得此类实例,可以从各种输入源解析XML。这些输入源是InputStreams,Files,URL和SAX InputSources。
----其构造方法是受保护的
1 public static void main(String[] args) throws Exception { 2 // 创建解析工厂 3 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 4 // 创建解析器 5 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); 6 // 通过解析器读取配置文件--返回Document对象树--org.w3c.dom.Document 7 Document doc = documentBuilder.parse("config/bookstore.xml"); 8 9 // 创建XPath对象 10 XPathFactory xPathFactory = XPathFactory.newInstance(); 11 XPath xPath = xPathFactory.newXPath(); 12 /* 13 * 1.获取bookstore节点下的book属性category值为web下的第二个 title节点的文本内容 14 * bookstore-》book[@category='web'][2]-》title 15 * xpath路径:/bookstore/book[@category='web'][2]/title/text() 16 */ 17 String titleXpath = "/bookstore/book[@category='web'][2]/title/text()"; 18 String titleVlue = (String) xPath.evaluate(titleXpath, doc, XPathConstants.STRING); 19 System.out.println("1:" + titleVlue); 20 /* 21 * 2.获取bookstore节点下book属性category值为web的title属性为en的节点内容 22 * xpath路径:/bookstore/book[@category='web']/title[@lang='en']/text() 选取属性为en 23 * 的title[@lang='en'] 24 */ 25 String titleXpath2 = "/bookstore/book[@category='web']/title[@lang='en']/text()"; 26 String titleValue2 = (String) xPath.evaluate(titleXpath2, doc, XPathConstants.STRING); 27 System.out.println("2:" + titleValue2); 28 /* 29 * 3.获取bookstore下book属性category值为cooking的title的lang属性的值 30 * xpath路径:/bookstore/book[@category='cooking']/title/@lang 31 */ 32 // 获取属性的值 33 String str1 = "/bookstore/book[@category='cooking']/title/@lang"; 34 String shuxing = (String) xPath.evaluate(str1, doc, XPathConstants.STRING); 35 System.out.println("3:" + shuxing); 36 /* 37 * 4.获取bookstore节点下的所有book的节点集合 /bookstore/book 38 */ 39 // 返回值是节点集合 40 String str3 = "/bookstore/book"; 41 NodeList all = (NodeList) xPath.evaluate(str3, doc, XPathConstants.NODESET); 42 // 开始遍历集合 43 for (int i = 0; i < all.getLength(); i++) { 44 // book节点 45 Element bookelt = (Element) all.item(i);// i是获取第几个 46 String titleValue = (String) xPath.evaluate("title", bookelt, XPathConstants.STRING); 47 String authorValue = (String) xPath.evaluate("author", bookelt, XPathConstants.STRING); 48 String yearValue = (String) xPath.evaluate("year", bookelt, XPathConstants.STRING); 49 String priceValue = (String) xPath.evaluate("price", bookelt, XPathConstants.STRING); 50 51 System.out.println( 52 "title:" + titleValue + " author:" + authorValue + " year:" + yearValue + " price:" + priceValue); 53 54 } 55 }
//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>