当前位置:网站首页>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 )
边栏推荐
- MySQL error 28 and solution
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- .net core 关于redis的pipeline以及事务
- FCOS3D label assignment
- Learning breakout 2 - about effective learning methods
- 2022-7-6 Leetcode 977.有序数组的平方
- flask session伪造之hctf admin
- Environment configuration of lavarel env
- Navicat运行sql文件导入数据不全或导入失败
- [fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
猜你喜欢
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
最佳实践 | 用腾讯云AI意愿核身为电话合规保驾护航
2022-7-6 Leetcode 977. Square of ordered array
MySQL error 28 and solution
Use of polarscatter function in MATLAB
【堡垒机】云堡垒机和普通堡垒机的区别是什么?
《厌女:日本的女性嫌恶》摘录
2022-7-7 Leetcode 844.比较含退格的字符串
AI人才培育新思路,这场直播有你关心的
Did login metamask
随机推荐
Is the compass stock software reliable? Is it safe to trade stocks?
[untitled]
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
Selenium库
Deep understanding of array related problems in C language
What parameters need to be reconfigured to replace the new radar of ROS robot
Laravel Form-builder使用
现在网上开户安全么?那么网上开户选哪个证券公司?
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
648. 单词替换 : 字典树的经典运用
Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
Solve the cache breakdown problem
室内ROS机器人导航调试记录(膨胀半径的选取经验)
move base参数解析及经验总结
手里的闲钱是炒股票还是买理财产品好?
2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
PHP中用下划线开头的变量含义
Mysql怎样控制replace替换的次数?
2022-7-7 Leetcode 844. Compare strings with backspace