Method:
public static map<string, string> Changemap (String xmldoc) {
if (!xmldoc.equals ("") && (xmldoc! = null)) {
Create a new string
StringReader xmlstring = new StringReader (xmldoc);
Creating a new input source The SAX parser uses the InputSource object to determine how to read the XML input
InputSource Source = new InputSource (xmlstring);
Create a new Saxbuilder
Saxbuilder SAXB = new Saxbuilder ();
Defining the Return Object
map<string, string> reMap = new hashmap<string, string> ();
try {
Constructs a document from the input source
Document doc = saxb.build (source);
The root element taken
Element root = Doc.getrootelement ();
Gets the collection of all child elements of the root element
List node = Root.getchildren ();
Element et = null;
for (int i = 0; i < node.size (); i++) {
Loops get the child elements in turn
ET = (Element) node.get (i);
Get the Inner child node
List subnode = Et.getchildren ();
if (subnode.size () = = 0) {
Load into map
Remap.put (Et.getname (), Et.gettext ());
} else {
Element Subet = null;
for (int j = 0; J < Subnode.size (); j + +) {
Loops get the child elements in turn
Subet = (Element) subnode.get (j);
Remap.putall (Changemap (ReMap, Subet));
ReMap = Changemap (ReMap, Subet);
}
}
}
} catch (Exception e) {
Log.error ("\ n return XML parse failed," + e.getmessage ());
}
return REMAP;
} else {
return null;
}
}
/**
*
* @description recursive parsing of XML
* @param map
* @param et
* @return Map<string, string>
*/
@SuppressWarnings ("Unchecked")
public static map<string, string> Changemap (map<string, string> Map,
Element ET) {
Defining the Return Object
map<string, string> reMap = new hashmap<string, string> ();
try {
Get the Inner child node
List subnode = Et.getchildren ();
Remap.putall (map);
if (subnode.size () = = 0) {
Load into map
if (Remap.containskey (Et.getname ())) {
String key = "";
for (int i = 0; i < i++) {
Key = Et.getname () + i;
if (Remap.containskey (key)) {
Continue
} else {
Remap.put (Key, Et.gettext ());
Break
}
}
} else {
Remap.put (Et.getname (), Et.gettext ());
}
} else {
Element Subet = null;
for (int i = 0; i < subnode.size (); i++) {
Loops get the child elements in turn
Subet = (Element) subnode.get (i);
ReMap = Changemap (ReMap, Subet);
}
}
} catch (Exception e) {
Log.error ("\ n return XML parse failed," + e.getmessage ());
}
return REMAP;
}
Convert XML to map