目標:使用JOM4j遍歷所有的元素節(jié)點,并且取出來其中的值,結(jié)果打印到控制臺。 源代碼如下:本程序依賴DOM4j包。
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: leizhimin
* Date: 2008-4-14 14:02:12
* Note: Java遞歸遍歷XML所有元素
*/
public class XmlTest {
// private static Map xmlmap = new HashMap();
//存儲xml元素信息的容器
private static List elemList = new ArrayList();
//要測試的xml對象
private static String srcXml = "\n" +
"\n" +
" \n" +
" 某人\n" +
" \n" +
" \n" +
" 10002\n" +
" 西安市太白路\n" +
" \n" +
" \n" +
" 10002\n" +
" 空ID節(jié)點啊\n" +
" \n" +
" \n" +
" 10002\n" +
" 空ID節(jié)點啊\n" +
" \n" +
"\t\t\t\n" +
"\t\t\t\t\n" +
" 西安市太白路2\n" +
" \n" +
"\t\t\n" +
" \n" +
" \n" +
" ASDF\n" +
" \n" +
"";
public static void main(String args[]) throws DocumentException {
XmlTest test = new XmlTest();
Element root = test.getRootElement();
test.getElementList(root);
String x = test.getListString(elemList);
System.out.println("-----------原xml內(nèi)容------------");
System.out.println(srcXml);
System.out.println("-----------解析結(jié)果------------");
System.out.println(x);
}
/**
* 獲取根元素
*
* @return
* @throws DocumentException
*/
public Element getRootElement() throws DocumentException {
Document srcdoc = DocumentHelper.parseText(srcXml);
Element elem = srcdoc.getRootElement();
return elem;
}
/**
* 遞歸遍歷方法
*
* @param element
*/
public void getElementList(Element element) {
List elements = element.elements();
if (elements.size() == 0) {
//沒有子元素
String xpath = element.getPath();
String value = element.getTextTrim();
elemList.add(new Leaf(xpath, value));
} else {
//有子元素
for (Iterator it = elements.iterator(); it.hasNext();) {
Element elem = (Element) it.next();
//遞歸遍歷
getElementList(elem);
}
}
}
public String getListString(List elemList) {
StringBuffer sb = new StringBuffer();
for (Iterator it = elemList.iterator(); it.hasNext();) {
Leaf leaf = it.next();
sb.append(leaf.getXpath()).append(" = ").append(leaf.getValue()).append("\n");
}
return sb.toString();
}
}
/**
* xml節(jié)點數(shù)據(jù)結(jié)構(gòu)
*/
class Leaf {
private String xpath; //
private String value;
public Leaf(String xpath, String value) {
this.xpath = xpath;
this.value = value;
}
public String getXpath() {
return xpath;
}
public void setXpath(String xpath) {
this.xpath = xpath;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
} |
運行結(jié)果: -----------原xml內(nèi)容------------
某人
10002
西安市太白路
10002
空ID節(jié)點啊
10002
空ID節(jié)點啊
西安市太白路2
ASDF
-----------解析結(jié)果------------
/doc/person/name = 某人
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 西安市太白路
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 空ID節(jié)點啊
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 空ID節(jié)點啊
/doc/person/adds/add/*[name()='BS'] =
/doc/person/adds/add/note = 西安市太白路2
/doc/other/name = ASDF |
Process finished with exit code 0 可以發(fā)現(xiàn),有很多xpath相同的值域。
【編輯推薦】
- 高手Java核心技術(shù)學習筆記
- 如何使用 JavaScript XSLT 處理 XML 文件
- JSP結(jié)合XML+XSLT將輸出轉(zhuǎn)換HTML
- Java通過JNI調(diào)用C語言的方法
- JAVA環(huán)境變量的設(shè)置