java - Creating XML- input data from HashMap -


i have below data in hashmap:

map<string, string> data = new hashmap<string, string>();          data.put("a.b.c.c1.c11", "c11");         data.put("a.b.c.c1.c12", "c12");         data.put("a.b.c.c2.c21", "c21");         data.put("a.b.c.c2.c22", "c22");         data.put("a.b.c.c3.c31", "c31");         data.put("a.b.c.d", "d"); 

i have requiremrnt create xml in below format(expected output):

<a>    <b>       <c>          <c1>             <c11>c11</c11>             <c12>c12</c12>          </c1>          <c2>             <c21>c21</c21>             <c22>c22</c22>          </c2>          <c3>             <c3>c3</c3>          </c3>          <d>d</d>       </c>    </b> </a> 

below output code generated:

<a>   <b>     <c>       <d>         <c1>           <c11>             <c12>               <c2>                 <c21>                   <c22>                     <c3>                       <c31/>                     </c3>                   </c22>                 </c21>               </c2>             </c12>           </c11>         </c1>       </d>     </c>   </b> </a> 

code(dirty) wrote:

public class dynamicxml3 {     public static   boolean isfirstelemnt=true;     static element element = null;     public static element mainroot =null;     public static void main(string args[]) {          try {             documentbuilderfactory docfactory = documentbuilderfactory.newinstance();             documentbuilder builder = docfactory.newdocumentbuilder();             document document = builder.newdocument();             string root,a1="a1";             map<string, string> data = new hashmap<string, string>();             data.put("a.b.c.c1.c11", "c11");             data.put("a.b.c.c1.c12", "c12");             data.put("a.b.c.c2.c21", "c21");             data.put("a.b.c.c2.c22", "c22");             data.put("a.b.c.c3.c31", "c31");             data.put("a.b.c.c.d", "d");              data = new treemap<string, string>(data);             set set1 = data.entryset();             iterator i1 = set1.iterator();             while(i1.hasnext()) {                 map.entry me1 = (map.entry)i1.next();                 system.out.println(me1.getkey() + ": ");                 //system.out.println(me1.getvalue());                  //create buddy xml                 string key= me1.getkey()+"";                 string[] buddyxml= key.split("\\.");                  nodelist rootelementlist = document.getelementsbytagname(buddyxml[0]);                 if(!(rootelementlist.getlength()>0)){                     mainroot = document.createelement(buddyxml[0]);                     document.appendchild(mainroot);                     system.out.println("root created\n");                 }                  for(int i=0; i<buddyxml.length; i++){                     system.out.println("value: "+buddyxml[i]);                     nodelist elementlist = document.getelementsbytagname(buddyxml[i]);                     system.out.println("here "+elementlist.getlength());                     system.out.println("creating node "+buddyxml[i]);                     if(!(elementlist.getlength()>0)){                         system.out.println("creating node "+buddyxml[i] + " & main root: "+mainroot.getnodename());                         element element =createnode(buddyxml[i], document, mainroot);                         mainroot = element;                     }                 }                 system.out.println("");             }              transformerfactory tfactory = transformerfactory.newinstance();             transformer transformer = tfactory.newtransformer();             // add indentation outputs             transformer.setoutputproperty(outputkeys.indent, "yes");             transformer.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", "2");             domsource source = new domsource(document);             streamresult result = new streamresult(new file("d://rc/buddycode.xml"));             transformer.transform(source, result);          } catch (exception e) {             system.out.println("exception: "+e);         }     }     public static void createelement(element elemnetname, document document, object data) {         text text =document.createtextnode(data.tostring());         elemnetname.appendchild(text);     }      private static element createnode(string nodename, document document,element rootelement) {         element node = document.createelement(nodename);         rootelement.appendchild(node);         return node;     } } 

this not working expected, tried change other appraoch doesn't seems work, guide me on same.

note: data in hashmap may incease horizontally , vertically, means, @ run time can have number of node, requirement create generic code.prefered use domparser.

can try :

public class dynamicxml3 {      private static final string separtor = "\\.";      public static void main(string args[]) {          try {             documentbuilderfactory docfactory = documentbuilderfactory.newinstance();             documentbuilder builder = docfactory.newdocumentbuilder();             document document = builder.newdocument();              map<string, string> data = new hashmap<string, string>();             data.put("a.b.c.c1.c11", "c11");             data.put("a.b.c.c1.c12", "c12");             data.put("a.b.c.c2.c21", "c21");             data.put("a.b.c.c2.c22", "c22");             data.put("a.b.c.c3.c31", "c31");             data.put("a.b.c.c.d", "d");              iterator<entry<string, string>> = data.entryset().iterator();             while (it.hasnext()) {                 map.entry<string, string> pair = (map.entry<string, string>)it.next();                 system.out.println(pair.getkey() + " = " + pair.getvalue());                 appendelement(document, pair.getkey(), pair.getvalue());                 it.remove();             }              transformerfactory tfactory = transformerfactory.newinstance();             transformer transformer = tfactory.newtransformer();             transformer.setoutputproperty(outputkeys.indent, "yes");             transformer.setoutputproperty("{http://xml.apache.org/xslt}indent-amount", "2");             domsource source = new domsource(document);             streamresult result = new streamresult(new file("c:/users/desktop/buddycode.xml"));             transformer.transform(source, result);          } catch (exception e) {             system.out.println("exception: "+e);         }     }      private static void appendelement(document document, string key, string value) {         string[] splitedelements = key.split(separtor);         (int = 0; < splitedelements.length; i++) {             nodelist nlst = document.getelementsbytagname(splitedelements[i]);             if(nlst == null || nlst.getlength() == 0) {                 if(i==0) {                     document.appendchild(document.createelement(splitedelements[i]));                 } else {                     (document.getelementsbytagname(splitedelements[i-1]).item(0)).appendchild(document.createelement(splitedelements[i]));                 }                 if(i==(splitedelements.length-1)) {                     (document.getelementsbytagname(splitedelements[i]).item(0)).appendchild(document.createtextnode(value));                 }             }         }     }  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -