Convert XML to map

Source: Internet
Author: User
Tags getmessage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.