-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerParser.java
More file actions
29 lines (27 loc) · 1.17 KB
/
ServerParser.java
File metadata and controls
29 lines (27 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package XML.Class;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @authtor liFei
* @date 2020/6/23-9:51
*/
public class ServerParser {
public static void main(String[] args) throws DocumentException {
//创建解析器
SAXReader saxReader=new SAXReader();
//通过解析器的reader将配置文件读取到内存中,生成一个Document[org.dom4j]对象树
Document document=saxReader.read("D:\\Practice\\JavaWeb\\src\\XML\\xml\\server.xml");
//获取connector结点对象的路径:server->service->connector
//获取connector结点元素对象的xpath路径:/server/service/connector server//connector //connector
Element connectorElt = (Element)document.selectSingleNode("server//connector");
//获取connectorELt结点元素对象的port属性对象
Attribute portAttr=connectorElt.attribute("port");
//获取portAttr属性对象的值
String port=portAttr.getStringValue();
System.out.println(port);
//System.out.println(connectorElt.attributeValue("port"));
}
}