Package com.hotmail.friendly;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import Org.dom4j.DocumentHelper;
Import org.dom4j.Element;
public class Xmltest {//private static map<string, string> XmlMap = new hashmap<string,//string> ();
containers that store XML element information private static list<leaf> elemlist = new arraylist<leaf> (); The XML object to be tested private static String Srcxml = "<?xml version=/" 1.0/"encoding=/" gbk/"+" < doc>/n "+" <person>/n "+" <name> someone </name>/n "+" <adds>
/n "+" <add id=/"10002/" >/n "+" <bs>10002</bs>/n "+ "<note> Xi ' an city Taibai road </note>/n" + "</add>/n" + "<add I d=/"/" >/n "+" <bs>10002</bs>/n "+" <note> NULL ID node AH </note>/n "+" </add>/n " + "<add>/n" + "<bs>10002</bs>/n" + "<n ote> NULL ID node AH </note>/n "+" </add>/n "+" <add id=/"10001/" >/n "+" <bs xmlns=/"10001/"/>/n "+" <note> Xi ' an city Taibai road 2</note>/n "+" </add >/n "+" </adds>/n "+" </person>/n "+" <other>/n "+" <nam
E id=/"hehe/" ">asdf</name>/n" + "</other>/n" + "</doc>"; /** * Get root element * * @return * @throws documentexception/Public element getrootelement () throws Document
Exception {Document Srcdoc = Documenthelper.parsetext (srcxml);
Element Elem = Srcdoc.getrootelement ();
return elem; /** * Recursive Traversal method * * @paraM element */public void Getelementlist (element Element) {List elements = element.elements ();
if (elements.size () = = 0) {//No child element String XPath = Element.getpath ();
String value = Element.gettexttrim ();
Elemlist.add (New Leaf (XPath, value)); else {//has child element for (Iterator it = Elements.iterator (); It.hasnext ();)
{element elem = (Element) It.next ();
Recursive traversal getelementlist (elem);
}} public String getliststring (list<leaf> elemlist) {stringbuffer sb = new StringBuffer (); for (iterator<leaf> it = Elemlist.iterator (); It.hasnext ();)
{The Leaf leaf = It.next ();
Sb.append (Leaf.getxpath ()). Append ("="). Append (Leaf.getvalue ()). Append ("/n");
return sb.tostring ();
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 ("-----------Original XML Content------------");
System.out.println (Srcxml);
System.out.println ("-----------analytic result------------");
SYSTEM.OUT.PRINTLN (x);
}} package com.hotmail.friendly;
/** * XML Node data structure * * public 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;
}
}