Java遞歸遍歷XML全部元素
作一個遞歸遍歷XML的例子,爲更爲複雜的解析工做作基礎。
目標:遍歷全部的元素節點,而且取出來其中的值,結果打印到控制檯。
源代碼以下:
本程序依賴DOM4j包。
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import java.util.*;
/**
* Created by IntelliJ IDEA.<br>
* <b>User</b>: leizhimin<br>
* <b>Date</b>: 2008-4-14 14:02:12<br>
* <b>Note</b>: Java遞歸遍歷XML全部元素
*/
public
class XmlTest {
// private static Map<String, String> xmlmap = new HashMap<String, String>();
//存儲xml元素信息的容器
private
static List<Leaf> elemList =
new ArrayList<Leaf>();
//要測試的xml對象
private
static String srcXml =
"<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
"<doc>\n" +
" <person>\n" +
" <name>某人</name>\n" +
" <adds> \n" +
" <add ID=\"10002\">\n" +
" <BS>10002</BS>\n" +
" <note>西安市太白路</note>\n" +
" </add>\n" +
" <add ID=\"\">\n" +
" <BS>10002</BS>\n" +
" <note>空ID節點啊</note>\n" +
" </add>\n" +
" <add>\n" +
" <BS>10002</BS>\n" +
" <note>空ID節點啊</note>\n" +
" </add>\n" +
"\t\t\t<add ID=\"10001\">\n" +
"\t\t\t\t<BS xmlns=\"10001\"/>\n" +
" <note>西安市太白路2</note>\n" +
" </add>\n" +
"\t\t</adds>\n" +
" </person>\n" +
" <other>\n" +
" <name ID=\"HEHE\">ASDF</name>\n" +
" </other>\n" +
"</doc>";
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內容------------");
System.out.println(srcXml);
System.out.println(
"-----------解析結果------------");
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<Leaf> elemList) {
StringBuffer sb =
new StringBuffer();
for (Iterator<Leaf> it = elemList.iterator(); it.hasNext();) {
Leaf leaf = it.next();
sb.append(leaf.getXpath()).append(
" = ").append(leaf.getValue()).append(
"\n");
}
return sb.toString();
}
}
/**
* xml節點數據結構
*/
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;
}
}
運行結果:
-----------原xml內容------------
<?
xml
version
="1.0"
encoding
="GBK"
?>
<
doc
>
<
person
>
<
name
>某人
</
name
>
<
adds
>
<
add
ID
="10002"
>
<
BS
>10002
</
BS
>
<
note
>西安市太白路
</
note
>
</
add
>
<
add ID=""
>
<
BS
>10002
</
BS
>
<
note
>空ID節點啊
</
note
>
</
add
>
<
add
>
<
BS
>10002
</
BS
>
<
note
>空ID節點啊
</
note
>
</
add
>
<
add
ID
="10001"
>
<
BS
xmlns
="10001"
/>
<
note
>西安市太白路2
</
note
>
</
add
>
</
adds
>
</
person
>
<
other
>
<
name
ID
="HEHE"
>ASDF
</
name
>
</
other
>
</
doc
>
-----------解析結果------------
/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節點啊
/doc/person/adds/add/BS = 10002
/doc/person/adds/add/note = 空ID節點啊
/doc/person/adds/add/*[name()='BS'] =
/doc/person/adds/add/note = 西安市太白路2
/doc/other/name = ASDF
Process finished with exit code 0
能夠發現,有不少xpath相同的值域。