java对xml解析_Java中对xml的解析
//?1.通過DocumentBuilderFactor創建解析工廠
DocumentBuilderFactory?builderFactory?=?DocumentBuilderFactory
.newInstance();
//?2.通過工廠獲得解析器
DocumentBuilder?builder?=?builderFactory.newDocumentBuilder();
//?3.通過parser方法獲取Document
Document?document?=?builder.parse("conf/books.xml");
//?4.獲取xpath對象
XPath?xpath?=?XPathFactory.newInstance().newXPath();
//?5.獲取bookstore節點下book屬性category值為web下的第二個title節點的文本內容
String?exp?=?"/bookstore/book[@category='web'][2]/title/text()";
String?tit?=?(String)?xpath.evaluate(exp,?document,
XPathConstants.STRING);
System.out.println(tit);
//?獲取bookstore節點下book屬性category值為web的titile屬性為en的節點內容
String?expEn?=?"/bookstore/book[@category='web']/title[@lang?='en']/text()";
String?titEN?=?(String)?xpath.evaluate(expEn,?document,
XPathConstants.STRING);
System.out.println(titEN);
//?獲取bookstore下book屬性category值為cooking的title的lang屬性的值
String?expLan?=?"/bookstore/book[@category='cooking']/title/@lang";
String?lang?=?(String)?xpath.evaluate(expLan,?document,
XPathConstants.STRING);
System.out.println(lang);
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java对xml解析_Java中对xml的解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java通信项目_Java项目中的多线程
- 下一篇: xml动态生成java_从XML文档动态