做一个递归遍历XML的例子,为更为复杂的解析工作做基础。 目标:遍历所有的元素节点,并且取出来其中的值,结果打印到控制台。 源代码如下:本程序依赖DOM4j包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
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 = "< ?xml version=\"1.0\" encoding=\"GBK\"?>\n" + "\n" + " \n" + " 某人\n" + " \n" + " \n" + " 10002\n" + " 西安市太白路\n" + " \n" + " \n" + " 10002\n" + " 空ID节点啊\n" + " \n" + " \n" + " 10002\n" + " 空ID节点啊\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内容------------"); 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 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节点数据结构 */ 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”?>
某人
10002
西安市太白路
10002
空ID节点啊
10002
空ID节点啊
西安市太白路2
ASDF
———–解析结果————
/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相同的值域。
Java基础:Dom4j递归遍历XML所有元素
发布时间:2009.06.16 05:05 来源:51cto blog 作者: dorothyfeng
做一个递归遍历XML的例子,为更为复杂的解析工作做基础。 目标:遍历所有的元素节点,并且取出来其中的值,结果打印到控制台。 源代码如下:本程序依赖DOM4j包。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
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 = "< ?xml version=\"1.0\" encoding=\"GBK\"?>\n" + "\n" + " \n" + " 某人\n" + " \n" + " \n" + " 10002\n" + " 西安市太白路\n" + " \n" + " \n" + " 10002\n" + " 空ID节点啊\n" + " \n" + " \n" + " 10002\n" + " 空ID节点啊\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内容------------"); 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 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节点数据结构 */ 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”?>
某人
10002
西安市太白路
10002
空ID节点啊
10002
空ID节点啊
西安市太白路2
ASDF
———–解析结果————
/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相同的值域。