当前位置:网站首页>Parsing of XML files
Parsing of XML files
2022-07-07 14:04:00 【Xiao afai_】
This issue is wonderful
Catalog
1、Java Three configuration locations and reading methods of configuration files in
Knowledge required
1、Java Three configuration locations and reading methods of configuration files in
Class name .class.getResourceAsStream("xxx"): Get the documents under the same package
Class name .class.getResourceAsStream("/xxx"): Get the file under the root directory
context.getResourceAsStream("/WIN-INF/xxx"): Get WIN-INF Safe path
2、dome4j Common methods
selectNodes: Get multiple elements
selectSingleNode: Get a single element
getRootElement(): Get the root element
attributeValue: Only elements can click this method to get values
getText: Get the element text
notes : Elements can be nodes , But the attribute in the element is a node, not an element
3、xpath grammar
/: Positioning path
@: attribute
analysis XML Code operation
1、 It needs to be parsed XML Case study (config.xml file )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<config>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
<action path="/studentAction02" type="org.lisen.mvc.action.StudentAction">
<forward name="students02" path="/students/studentList.jsp" redirect="false"/>
</action>
</config>
2、 Before writing parsing code, you need to import jar package
3、 Write the code of parsing operation
public class XmlReader {
public static void main(String[] args) throws Exception {
InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(in);
Element rootElement = doc.getRootElement();
List<Element> actions = rootElement.selectNodes("action");
for(Element e: actions) {
String path = e.attributeValue("path");
String type = e.attributeValue("type");
System.out.println("action path = "+path);
System.out.println("action type = "+type);
List<Element> forwards = e.selectNodes("forward");
for(Element f: forwards) {
String name = f.attributeValue("name");
String fPath = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
System.out.println("forward name = "+name);
System.out.println("forward fPath = "+fPath);
System.out.println("forward redirect = "+redirect);
}
System.out.println(" Parsing ended ");
}
}
}
Output results ( You can see that you have obtained the information provided above XML Relevant elements and nodes in the file )
边栏推荐
- 请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
- 实现IP地址归属地显示功能、号码归属地查询
- FCOS3D label assignment
- Help tenants
- Excerpt from "misogyny: female disgust in Japan"
- Use day JS let time (displayed as minutes, hours, days, months, and so on)
- call undefined function openssl_ cipher_ iv_ length
- Wired network IP address of VMware shared host
- Clickhouse (03) how to install and deploy Clickhouse
- Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
猜你喜欢
Transferring files between VMware and host
最长上升子序列模型 AcWing 1014. 登山
. Net core about redis pipeline and transactions
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
Thread pool reject policy best practices
室内ROS机器人导航调试记录(膨胀半径的选取经验)
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
2022-7-7 Leetcode 34.在排序数组中查找元素的第一个和最后一个位置
2022-7-6 Leetcode27.移除元素——太久没有做题了,为双指针如此狼狈的一天
随机推荐
内存溢出和内存泄漏的区别
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
FCOS3D label assignment
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
MySQL "invalid use of null value" solution
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Leecode3. Longest substring without repeated characters
The meaning of variables starting with underscores in PHP
交付效率提升52倍,运营效率提升10倍,看《金融云原生技术实践案例汇编》(附下载)
Transferring files between VMware and host
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
Environment configuration of lavarel env
【网络安全】sql注入语法汇总
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
Build a secure and trusted computing platform based on Kunpeng's native security
648. 单词替换 : 字典树的经典运用
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
Use of polarscatter function in MATLAB