?? 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 + -