当前位置:网站首页>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>
边栏推荐
- Solution: you can ping others, but others can't ping me
- Lumiprobe free radical analysis h2dcfda instructions
- Cache problems after app release
- 组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
- SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
- Lumiprobe 细胞成像研究丨PKH26细胞膜标记试剂盒
- Three simple methods of ES6 array de duplication
- 云服务器ECS夏日省钱秘籍,这次@老用户快来领走
- Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
- Appgallery connect scenario development practice - image storage and sharing
猜你喜欢
How to use the low code platform of the Internet of things for personal settings?
Nacos configuration file publishing failed, please check whether the parameters are correct solution
Improve yolov5 with gsconv+slim neck to maximize performance!
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
案例分享:QinQ基本组网配置
Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
网易游戏,激进出海
[to.Net] C set class source code analysis
制造业SRM管理系统供应商全方位闭环管理,实现采购寻源与流程高效协同
随机推荐
MATLAB中subplot函数的使用
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
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
Clean up system cache and free memory under Linux
Solution: you can ping others, but others can't ping me
水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
The difference between indexof and includes
Write it down once Net travel management background CPU Explosion Analysis
Supervarimag superconducting magnet system SVM series
【To .NET】C#集合类源码解析
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
论文泛读【FiLM: Visual Reasoning with a General Conditioning Layer】
How to use the low code platform of the Internet of things for personal settings?
Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
前4A高管搞代运营,拿下一个IPO
学习笔记【gumbel softmax】
Go语言高级
[to.Net] C set class source code analysis
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
精益思想:来源,支柱,落地。看了这篇文章就懂了