Parse xml in special format to map, parse xmlmap
Because of the special project, the xml document style to be parsed is special, so I wrote a method to parse special xml.
Provide xml styles first
<?xml version="1.0" encoding="UTF-8"?><root>
If you see this xml, you will know where it is special. First, the head is normal. The label name is the key of the map and the content is the value. But part of the body is the rule, however, the other part is the detail label of the details package. The value of the package in the <gradeitem> H </gradeitem> label is the key of the map, the value in the <gradevalue> 9 </gradevalue> label is the value of map;
public static Map<String, String> xmlToMap(String data) { Map<String, String> map = new HashMap<String, String>(); ParseRatingData prd = new ParseRatingData(); Document doc; try { doc = DocumentHelper.parseText(data); Element root = doc.getRootElement(); } catch (DocumentException e) { e.printStackTrace(); } return map; }
First, it is necessary to obtain the root node. First, I want to parse the items in the details label, and then the rest is a rule, there are two ways to parse the xml
Public static Map <String, String> xmlToMap (String data) {Map <String, String> map = new HashMap <String, String> (); ParseRatingData prd = new ParseRatingData (); document doc; try {doc = policenthelper. parseText (data); Element root = doc. getRootElement (); // parse head Element head = root. element ("head"); map. putAll (prd. nodeToMap (head, "head", map); // parse body Element body = root. element ("body"); map. putAll (prd. nodeToMap (body, "body", map); // parse details Element details = body. element ("details"); List <Element> detailList = details. elements ("detail"); for (Element detail: detailList) {map. put (detail. elementText ("gradeitem"), detail. elementText ("gradevalue");} catch (incluentexception e) {e. printStackTrace ();} return map ;}
This is all the code of the method just now. In the detail parsing section, we first get all the detail under the details and put it in a list set, then, the values under Different tags are obtained as the key and value of map respectively.
Careful students have discovered the nodeToMap (Element ele, String str, Map map) method. Let's take a look at what this method has written.
Public Map <String, String> nodeToMap (Element node, String str, Map <String, String> map) {if (! Str. equals (node. getName () {if ("details ". equals (node. getName () {return map;} map. put (node. getName (), node. getTextTrim (); // name of the current node, content of the current node} // recursively traverse all the subnodes of the current node List <Element> listElement = node. elements (); // list for (Element e: listElement) of all level-1 subnodes {// traverse all level-1 subnodes nodeToMap (e, "", map ); // recursion} return map ;}
This is another common parsing method. It uses recursion to traverse all the subnodes under the input node parameter, and then uses the label name of the node as the key of the map, value as the value of map
Note that for convenience, the str In the parameter is actually the String format passed in to the node parameter. If it is the same, it is the parent node and will not be placed in the map, when they are different, the sub-nodes will be placed in the map.
If the details node appears, it means that the body traversal is pulled down here. Next, the details traversal needs to be processed. just return it directly, here, the map parameter is passed in to add data directly on the basis of the original map.
After the analysis is complete, please kindly advise me if you have any comments ~
You are welcome to repost. Please indicate the source for reprinting.
From getting started to giving up Java, MySQL is moving from database deletion to running ~~~