当前位置:网站首页>Dom4j parsing XML, XPath retrieving XML
Dom4j parsing XML, XPath retrieving XML
2022-07-01 19:25:00 【Gentle ~】
summary
Two ways of parsing
1.SAX analysis
2.DOM analysis
Dom Common parsing tools
DOM Parsing document object model
Dom4J analysis XML file
Dom4J Official website : https://dom4j.github.io/
Implementation steps
1. download Dom4j frame ;
2. Create a folder in the project :lib;
3. take dom4j-2.1.1.jar File copy to lib Folder ;
4. stay jar Right click on the file , choice Add as Library -> Click on OK;
5. Use of import package in class ;
Constructors 、 Method
Dom4J The analytic thought of the theory
Get document object Document, Get the element object and content from it .
SAXReader class
Constructors | explain |
---|---|
public SAXReader() | establish Dom4J Parser object for |
Document read(String url) | load XML The file becomes Document object |
Document class
Method | explain |
---|---|
Element getRootElement() | Get the root element object |
List<Element> elements() | Get all child elements under the current element |
List<Element> elements(String name) | Get the child element with the specified name under the current element and return the collection |
Element element(String name) | Get the child element with the specified name under the current element , If there are many people with the same name, return the first |
String getName() | Get the element name |
String attributeValue(String name) | Get the attribute value directly through the attribute name |
String elementText( Child element name ) | Get the text of the child element with the specified name |
String getText() | Get the text |
Code example
Dom4JDemo1.java
public class Dom4JDemo1 {
@Test
public void parseXMLData() throws Exception {
// 1、 Create a Dom4j Parser object for , Represents the whole dom4j frame
SAXReader saxReader = new SAXReader();
// 2、 hold XML The file is loaded into memory and becomes a Document Document object
// Document document = saxReader.read(new File("xml-app\\src\\Contacts.xml")); // You need to locate by module name
// Document document = saxReader.read(new FileInputStream("xml-app\\src\\Contacts.xml"));
// Be careful : getResourceAsStream Medium / Is directly to src Find the file under
InputStream is = Dom4JDemo1.class.getResourceAsStream("/Contacts.xml");
Document document = saxReader.read(is);
// 3、 Get the root element object
Element root = document.getRootElement();
System.out.println(root.getName());
// 4、 Take all child element objects under the root element ( Class A )
// List<Element> sonEles = root.elements();
List<Element> sonEles = root.elements("contact");
for (Element sonEle : sonEles) {
System.out.println(sonEle.getName());
}
// Take a child element
Element userEle = root.element("user");
System.out.println(userEle.getName());
// The first child element object is extracted by default (Java Language .)
Element contact = root.element("contact");
// Get sub element text
System.out.println(contact.elementText("name"));
// Remove space before and after
System.out.println(contact.elementTextTrim("name"));
// Get the sub element object under the current element
Element email = contact.element("email");
System.out.println(email.getText());
// Remove space before and after
System.out.println(email.getTextTrim());
// Get the attribute value according to the element
Attribute idAttr = contact.attribute("id");
System.out.println(idAttr.getName() + "-->" + idAttr.getValue());
// Extract attribute values directly
System.out.println(contact.attributeValue("id"));
System.out.println(contact.attributeValue("vip"));
}
}
Contacts.xml
<?xml version="1.0" encoding="UTF-8"?>
<contactList>
<contact id="1" vip="true">
<name> Pan Jinlian </name>
<gender> Woman </gender>
<email>[email protected]</email>
</contact>
<contact id="2" vip="false">
<name> Wusong </name>
<gender> male </gender>
<email>[email protected]</email>
</contact>
<contact id="3" vip="false">
<name> Wu Dawang </name>
<gender> male </gender>
<email>[email protected]</email>
</contact>
<user>
</user>
</contactList>
XML Search Technology :Xpath
XPath Introduce
XPath Use path expressions to locate XML Element node or attribute node in the document ,Xpath Technology is more suitable for information retrieval .
Usage flow
1. Import jar package (dom4j and jaxen-1.1.2.jar),Xpath Technology depends on Dom4j technology ;
2. adopt dom4j Of SAXReader obtain Document object ;
3. utilize XPath Provided API, combination XPath Complete the selection with the syntax of XML Parse the document element node ;
Method | explain |
---|---|
Node selectSingleNode(" expression ") | Get the unique element that matches the expression |
List<Node> selectNodes(" expression ") | Get a collection of elements that match the expression |
Xpath Search method of
1. Absolute path
/ Root element / Subelement / Sun element
Use the absolute path to get the list of search nodes layer by layer from the root node and return information .
Start with the root element , Look down one level at a time , You can't cross levels .
2. Relative paths
./ Subelement / Sun element
First get the root node , Then the relative path is used to obtain the child nodes of the next level node and return information .
Start with the current element , Look down one level at a time , You can't cross levels .
3. Full text search
Direct full-text search of all specified elements and return .
4. Property search
Search for properties in full text , Or elements with attributes .
Code example
XPathDemo.java
public class XPathDemo {
/** 1. Absolute path : / Root element / Subelement / Subelement . */
@Test
public void parse01() throws Exception {
// a、 Create parser object
SAXReader saxReader = new SAXReader();
// b、 hold XML Load as Document Document object
Document document =
saxReader.read(XPathDemo.class.getResourceAsStream("/Contacts2.xml"));
// c、 Retrieve all names
List<Node> nameNodes = document.selectNodes("/contactList/contact/name");
for (Node nameNode : nameNodes) {
Element nameEle = (Element) nameNode;
System.out.println(nameEle.getTextTrim());
}
}
/** 2. Relative paths : ./ Subelement / Subelement . (. Represents the current element ) */
@Test
public void parse02() throws Exception {
// a、 Create parser object
SAXReader saxReader = new SAXReader();
// b、 hold XML Load as Document Document object
Document document =
saxReader.read(XPathDemo.class.getResourceAsStream("/Contacts2.xml"));
Element root = document.getRootElement();
// c、 Retrieve all names
List<Node> nameNodes = root.selectNodes("./contact/name");
for (Node nameNode : nameNodes) {
Element nameEle = (Element) nameNode;
System.out.println(nameEle.getTextTrim());
}
}
/** 3. Full text search : // Elements Find this element in the full text // Elements 1/ Elements 2 Find elements in the full text 1 The following primary elements 2 // Elements 1// Elements 2 Find elements in the full text 1 All the elements below 2 */
@Test
public void parse03() throws Exception {
// a、 Create parser object
SAXReader saxReader = new SAXReader();
// b、 hold XML Load as Document Document object
Document document =
saxReader.read(XPathDemo.class.getResourceAsStream("/Contacts2.xml"));
// c、 Retrieving data
//List<Node> nameNodes = document.selectNodes("//name");
// List<Node> nameNodes = document.selectNodes("//contact/name");
List<Node> nameNodes = document.selectNodes("//contact//name");
for (Node nameNode : nameNodes) {
Element nameEle = (Element) nameNode;
System.out.println(nameEle.getTextTrim());
}
}
/** 4. Property search . //@ The attribute name Retrieve property objects in full text . // Elements [@ The attribute name ] Retrieve the element object containing this attribute in full text . // Elements [@ The attribute name = value ] Search the element object containing the attribute and the attribute value is the value in the full text . */
@Test
public void parse04() throws Exception {
// a、 Create parser object
SAXReader saxReader = new SAXReader();
// b、 hold XML Load as Document Document object
Document document =
saxReader.read(XPathDemo.class.getResourceAsStream("/Contacts2.xml"));
// c、 Retrieving data
List<Node> nodes = document.selectNodes("//@id");
for (Node node : nodes) {
Attribute attr = (Attribute) node;
System.out.println(attr.getName() + "===>" + attr.getValue());
}
// Inquire about name Elements ( contain id Attribute )
// Node node = document.selectSingleNode("//name[@id]");
Node node = document.selectSingleNode("//name[@id=888]");
Element ele = (Element) node;
System.out.println(ele.getTextTrim());
}
}
Contacts2.xml
<?xml version="1.0" encoding="UTF-8"?>
<contactList>
<contact id="1" vip="true">
<name> Pan Jinlian </name>
<gender> Woman </gender>
<email>[email protected]</email>
</contact>
<contact id="2" vip="false">
<name> Wusong </name>
<gender> male </gender>
<email>[email protected]</email>
</contact>
<contact id="3" vip="false">
<name> Wu Dawang </name>
<gender> male </gender>
<email>[email protected]</email>
</contact>
<user>
<contact>
<info>
<name id="888"> I'm XiMenqing </name>
</info>
</contact>
</user>
</contactList>
边栏推荐
- Lumiprobe cell imaging study PKH26 cell membrane labeling kit
- [quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
- 前4A高管搞代运营,拿下一个IPO
- Summary of cases of players' disconnection and reconnection in Huawei online battle service
- Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
- AppGallery Connect场景化开发实战—图片存储分享
- Gameframework eating guide
- B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
- MySQL常用图形管理工具 | 黑马程序员
- 记一次 .NET 差旅管理后台 CPU 爆高分析
猜你喜欢
June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries
Huawei game failed to initialize init with error code 907135000
Getting started with kubernetes command (namespaces, pods)
市值蒸发740亿,这位大佬转身杀入预制菜
Cdga | if you are engaged in the communication industry, you should get a data management certificate
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
Summary of the core steps in the life cycle of beans
Dom4J解析XML、Xpath检索XML
Lake shore optimag superconducting magnet system om series
Learn MySQL from scratch - database and data table operations
随机推荐
市值蒸发740亿,这位大佬转身杀入预制菜
赋能「新型中国企业」,SAP Process Automation 落地中国
Go language self-study series | go language data type
CDGA|从事通信行业,那你应该考个数据管理证书
Altair HyperWorks 2022 software installation package and installation tutorial
学习笔记【gumbel softmax】
华为游戏初始化init失败,返回错误码907135000
The former 4A executives engaged in agent operation and won an IPO
Example explanation: move graph explorer to jupyterlab
Contos 7 set up SFTP to create users, user groups, and delete users
[pytorch record] distributed training dataparallel and distributeddataparallel of the model
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
混沌工程平台 ChaosBlade-Box 新版重磅发布
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
案例分享:QinQ基本组网配置
C端梦难做,科大讯飞靠什么撑起10亿用户目标?
前4A高管搞代运营,拿下一个IPO
线程的并行、并发、生命周期
XML语法、约束