Package Test;
Import Java.io.StringReader;
Import Java.util.HashMap;
Import java.util.List;
Import org.jdom.Document;
Import org.jdom.Element;
Import org.jdom.JDOMException;
Import Org.jdom.input.SAXBuilder;
Import Org.xml.sax.InputSource;
public class Demo111 {
Static hashmap<string, object> result = new hashmap<string, object> ();
public static void Main (string[] args) {
String xml = "<?xml version=\" 1.0\ "encoding=\" utf-8\ "?>" +
"<Message>" +
"<Head>" +
"<_TransactionId>1</_TransactionId>" +
"<requestHeader><version>1.0</version><serialNo></serialNo><requestId>< /requestid><refserialno></refserialno><requesttime>2015-04-28 10:58:23.040</requestTime ><timeouttime></timeouttime><channelid></channelid><channelserialno></ Channelserialno> "+
"</requestHeader>" +
"<control><requestername></requestername><requesterrole>123010003</requesterrole ><requesterInsitution></requesterInsitution><requesterId></requesterId>< Requestertime>2015-04-28 10:58:23.040</requestertime><requesterlanguage>124010002</ Requesterlanguage><requesterlocale></requesterlocale><pagestartindex></pagestartindex ><pageEndIndex></pageEndIndex><availableResultsCount></availableResultsCount>< returnavailableresultcount></returnavailableresultcount><isauthorize></isauthorize>< Transcode></transcode> "+
"</control>" +
"</Head>" +
"<Body>" +
"<adminSysId>1001920073</adminSysId>" +
"<adminSysTypeCode>0</adminSysTypeCode>" +
"<custMsgList>" +
"<body001>body001</body001><body002>body002</body002>" +
"</custMsgList>" +
"</Body>" +
"</Message>";
SYSTEM.OUT.PRINTLN (XML);
/*********************** parsing string****************************/
StringReader read = new StringReader (XML);
InputSource Source = new InputSource (read);
Saxbuilder sb = new Saxbuilder ();
try {
Document doc = (document) sb.build (source);
Element root = Doc.getrootelement ();
Result.put (Root.getname (), Root.gettext ());
Parse (root);
} catch (Jdomexception e) {
E.printstacktrace ();
} catch (Exception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN (result);
}
public static hashmap<string,object> parse (Element root) {
List nodes = Root.getchildren ();
int len = Nodes.size ();
if (len==0) {
Result.put (Root.getname (), Root.gettext ());
} else {
for (int i=0;i<len;i++) {
element element = (Element) nodes.get (i);//loop Gets the child element in turn
Result.put (Element.getname (), Element.gettext ());
Parse (element);
}
}
return result;
}
}
Parse a string in XML format and deposit it in map