标签:ble art 解析 get read clip 生成树 dom tab
XML
1. XML 是 可扩展标识语言Extensible Markup Language, 在xml命名规则的基础上,根据自己的需求定义自己的标签
成对出现,
2. XML 文件的作用: 存储数据, 配置文件
3. 解析 xml 文件的方法: DOM, DOM4J, SAX
前两个是一次性读取到内存中,然后进行解析, SAX是 边读边解析, 大的配置文件 事件驱动
*** DOM4J 解析 xml 文件
包: dom4j-1.6.1.jar
SAXReader: 解析器,生成树对象
Document: 一个 xml 文档对象树,
Element: 元素节点。通过 Document 对象
创建 解析器,
生成树对象通过
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student> // 元素节点
<name>唐僧</name>
<age>110</age>
<notes>师傅</notes>
</student>
<student>
<name>孙悟空</name>
<age>100</age>
<notes>大师兄</notes>
</student>
</students>
** Myeclipse 解析 xml文件
更换工作空间: File -> switch workspace
设置字符集: preferences -> general - workspace text file encoding -> utf-8
设置JDK 版本:preferences -> java -> compiler(installed jres)
** 创建工程 java project ctrl +n prj-xml
新建文件夹 lib 导入 dom4j-1.6.1.jar
新建包 com.pw.xml
新建文件夹 conf 放置xml文件 students.xml
创建class Dom4jTest
public class Dom4jTest {
public static void main(String args[]){
try{
// 创建解析器
SAXReader reader = new SAXReader();
// 通过解析器的read方法将配置文件读取到内存中,生成一个Document[org.dom4j]对象树
Document document = reader.read("conf/students.xml");
// 获取根节点
Element root = document.getRootElement();
for(Iterator<Element> rootIter = root.elementIterator();rootIter.hasNext();){
Element studentElt = rootIter.next()
for(Iterator<Element> innerIter = studentElt.elementIterator();innerIter.hasNext();){
Element innerElt = innerIter.next();
String innerValue = innerElt.getStringValue();
System.out.println(innerValue);
}
System.out.println("----------------------")
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
*** SAX 解析 xml 文件
无需将整个文档加载到内存中, 内存占用下
创建解析工厂SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
创建解析器
执行 parse 方法 参数: File f, DefaultHandler dh 解析器,处理程序
开始标签 startElement() qName 标签名
characters() 将文本返回
结束标签 endElement(String uri, String localName, String qName)
原样输出配置文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student> // 元素节点
<name>唐僧</name>
<age>110</age>
<notes>师傅</notes>
</student>
<student>
<name>孙悟空</name>
<age>100</age>
<notes>大师兄</notes>
</student>
</students>
public class MySAXParser {
public static void main
try{
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
saxParser.parse("conf/persons.xml",new MyDefaultHandler());
}
catch(Exception e){
e.printStackTrace();
}
class MyDefaultHandler extends DefaultHandler {
@override
public void startElement(){
System.out.println("<" + qName + ">");
}
characters()
System.out.println(new String(ch, start, length));
endElement()
System.out.println("</" + qName + ">");
}
}
****XPath
XPath 使用
<config>
<database-info>
<driver-name>
<url>
<user>
<password>
</config>
try{
SAXReader reader = new SAXReader();
Doucument document = reader.read("conf/sys-config.xml");
// driver-name 节点元素的路径 config-> database-info -> driver-name
// XPath /config/database-info/driver-name
Element driverNameElt = (Element) document.selectSingleNode("/config/database-info/driver-name");
// 获取 driverNameElt 节点元素对象的文本内容
String driverName = driverNameElt.getStringValue();
// url 节点元素的路径 /config/database-info/url config//url //url
Element urlElt = (Element) document.selectSingleNode("/config/database-info/url");
String url = urlElt.getStringValue();
Element userElt = (Element) document.selectObject("/config/database-info/user");
String user = userElt.getText();
Element userElt = (Element) document.selectObject("/config/database-info/user");
String user = userElt.getTextTrim();
}
catch(){
}
*** 使用 Xpath 对象解析 XML 文件 (DocumentBuilder 解析器)
<bookstore>
<book category="web"> // 元素节点
<title lang="en">Learn XML</title>
<author >Erik T. Ray</author >
<year>2003</year>
<price>30.04</price>
</book>
<book category="web"> // 元素节点
<title lang="ur">Python</title>
<author >Erik T. Ray</author >
<year>2003</year>
<price>30.04</price>
</book>
<book category="cooking"> // 元素节点
<title lang="en">Cook </title>
<author >Erik T. Ray</author >
<year>2003</year>
<price>30.04</price>
</book>
</bookstore>
<!--
获取bookstore 节点下 book 属性 category 值为 web 下的 第二个title节点的文本内容
-->
bookstore -> book[@category=‘web‘][2] -> title
/bookstore/book[@category=‘web‘][2]/title/text()
获取bookstore 节点下 book 属性 category 值为 web 下的 title属性 为en的节点内容
bookstore -> book[@category=‘web‘] -> title[@lang=‘en‘]
bookstore/book[@category=‘web‘]/title[@lang=‘en‘] /text()
获取bookstore 节点下 book 属性 category 值为cooking 的 lang属性 的值
bookstore -> book[@category=‘cooking‘] -> title -> @lang
bookstore/book[@category=‘cooking‘]/title/@lang
获取bookstore 节点下 所有 book的节点集合
bookstore -> book
bookstore/book
public class MyXPathTest {
try {
// 创建解析工厂
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
// 创建解析器
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
// 通过解析器读取配置文件, 生成一个 Document[org.w3c.dom] 对象树
Document document = builder.parse("conf/bookstore.xml");
// 创建 XPath 对象
XPath xPath = XPathFactory.newInstance().newXPath();
// 返回 Object
String titleXpath = "/bookstore/book[@category=‘web‘][2]/title/text()"
String titleValue = (String)xPath.evaluate(titleXpath, document, XPathConstants.STRING);
System.out.println(titleValue);
String titleLangXpath = "bookstore/book[@category=‘web‘]/title[@lang=‘en‘] /text()"
String titleLangValue = (String)xPath.evaluate(titleLangXpath, document, XPathConstants.STRING);
System.out.println(titleLangValue);
String titleLangAttrXpath = "bookstore/book[@category=‘cooking‘]/title/@lang"
String titleLangAttrValue = (String)xPath.evaluate(titleLangAttrXpath, document, XPathConstants.STRING);
System.out.println(titleLangAttrValue);
NodeList bookList = (NodeList)xPath.evaluate("/bookstore/book", document, XPathConstants.NODESET);
for (int i = 0; i< bookList.getLength(); i++){
Element bookElt = (Element) bookList.item(i);
String titleValue01 = (String)xPath.evaluate("title", bookElt, XPathConstants.STRING);
String authorValue01 = (String)xPath.evaluate("author", bookElt, XPathConstants.STRING);
String yearValue01 = (String)xPath.evaluate("year", bookElt, XPathConstants.STRING);
String priceValue01 = (String)xPath.evaluate("price", bookElt, XPathConstants.STRING);
System.out.println(titleValue01 + " " + authorValue01 + " " + yearValue01 + " " + priceValue01);
System.out.println("-----------------");
}
System.out.println(titleLangAttrValue);
}
catch(Exception e){
e.printStackTrace();
}
}
标签:ble art 解析 get read clip 生成树 dom tab
原文地址:https://www.cnblogs.com/kingdomer/p/9763234.html