Java XML parsing into map

Source: Internet
Author: User

1. Files that need to be parsed. xml

1<?xml version= "1.0" encoding= "UTF-8"?>2<request>3<realName>test</realName>4<identityNumber>411525152242417185276</identityNumber>5<phone>1314456788</phone>6<user>7<value>1444444</value>8<name>12334</name>9<aa>Ten One</aa> A<age>23</age> -</user> -</request>

2. Tool Kit

1  Packagecom.ynet.utils;2 3 Importorg.dom4j.Document;4 Importorg.dom4j.Element;5 6 ImportJava.util.*;7 8 /**9 * Created by Arya on 2017/11/3.Ten  */ One  Public classXmlutils { A      Public StaticMap<string, object>Dom2map (Document doc) { -map<string, object> map =NewHashmap<string, object>(); -         if(doc = =NULL) the             returnmap; -Element root =doc.getrootelement (); -          for(Iterator Iterator =root.elementiterator (); Iterator.hasnext ();) { -Element e =(Element) Iterator.next (); +List List =e.elements (); -             if(List.size () > 0) { + Map.put (E.getname (), Dom2map (e)); A}Else at Map.put (E.getname (), E.gettext ()); -         } -         returnmap; -     } -  -  in      Public StaticMap Dom2map (Element e) { -Map map =NewHashMap (); toList List =e.elements (); +         if(List.size () > 0) { -              for(inti = 0; I < list.size (); i++) { theElement iter =(Element) list.get (i); *List maplist =NewArrayList (); $ Panax Notoginseng                 if(Iter.elements (). Size () > 0) { -Map m =Dom2map (ITER); the                     if(Map.get (Iter.getname ())! =NULL) { +Object obj =Map.get (Iter.getname ()); A                         if(!obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) { theMaplist =NewArrayList (); + maplist.add (obj); - Maplist.add (m); $                         } $                         if(Obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) { -Maplist =(List) obj; - Maplist.add (m); the                         } - Map.put (Iter.getname (), maplist);Wuyi}Else the Map.put (Iter.getname (), m); -}Else { Wu                     if(Map.get (Iter.getname ())! =NULL) { -Object obj =Map.get (Iter.getname ()); About                         if(!obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) { $Maplist =NewArrayList (); - maplist.add (obj); - Maplist.add (Iter.gettext ()); -                         } A                         if(Obj.getclass (). GetName (). Equals ("Java.util.ArrayList")) { +Maplist =(List) obj; the Maplist.add (Iter.gettext ()); -                         } $ Map.put (Iter.getname (), maplist); the}Else the Map.put (Iter.getname (), Iter.gettext ()); the                 } the             } -}Else in Map.put (E.getname (), E.gettext ()); the         returnmap; the     } About}

3. Controller

1  PackageCom.ynet.controller;2 3 4 Importcom.ynet.utils.XmlUtils;5 Importorg.dom4j.Document;6 ImportOrg.dom4j.DocumentHelper;7 Importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;8 Importorg.springframework.web.bind.annotation.RequestMapping;9 ImportOrg.springframework.web.bind.annotation.RestController;Ten  One Importorg.dom4j.DocumentException; A  - ImportJava.io.FileInputStream; - Importjava.io.IOException; the ImportJava.util.Map; -  - /** - * Created by Arya on 2017/11/3. +  */ - @RestController + @EnableAutoConfiguration A  Public classXmldemocontroller { at@RequestMapping (value= "/xmldemo") -      PublicMap<string, object>Xmldocumentfile () { -Document doc =NULL; -         Try { -FileInputStream FIS =NewFileInputStream ("F:\\myweb\\gateway\\gateway2\\src\\main\\resources\\customer.xml"); -             byte[] B =New byte[Fis.available ()]; in Fis.read (b); -String str =NewString (b); toDoc =Documenthelper.parsetext (str); +}Catch(IOException e) { - e.printstacktrace (); the}Catch(documentexception e) { * e.printstacktrace (); $         }Panax Notoginseng  -         returnXmlutils.dom2map (DOC); the     } +}

4.postman test Results

Java XML parsing into 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.