📄 readxml.java
字号:
package com.xml;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import java.util.*;
public class ReadXml {
/* 用于存放非oray元素标签 */
List<Map> liststr = new ArrayList<Map>();
/* List用于存放HashMap数据类型 */
List<Map> list = new ArrayList<Map>();
HashMap<String, String> ht = null;
Document doc = null;
public ReadXml() {
}// 无参构造函数
// 读取传入的XML文档
public void read(String filename) throws Exception {
SAXReader reader = new SAXReader();
doc = reader.read(new File(filename));
}
// 传入根原色启动递归遍历
public void treeWalk() {
treeWalk(doc.getRootElement());
}
public void treeWalk(Element element) {
for (int i = 0, size = element.nodeCount(); i < size; i++) {
Node node = element.node(i);
if (node instanceof Element) {
if (node.getName().endsWith("oary"))
readoary((Element) node);
else {
ht = new HashMap<String, String>();
ht.put(node.getName(), node.getText());
liststr.add(ht);
}
} else {
ht = new HashMap<String, String>();
ht.put(node.getName(), node.getText());
liststr.add(ht);
}
}
}
/** 专用读取oray标签内的子元素 */
public void readoary(Element element) {
for (int i = 0, size = element.nodeCount(); i < size; i++) {
Node node = element.node(i);
if (node.getText().trim().length() > 0) {
ht = new HashMap<String, String>();
ht.put(node.getName(), node.getText());
}
list.add(ht);
}
}
public List getoary() {
return list;
}
public List getother() {
return liststr;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -