title: Creating XML- input data from HashMap
body: <p>I have a below data in HashMap:</p>
  
  <pre><code>Map&lt;String, String&gt; data = new HashMap&lt;String, String&gt;();
  
          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");
  </code></pre>
  
  <p>I have a requiremrnt to create a XML in below format(expected output):</p>
  
  <pre><code>&lt;A&gt;
     &lt;B&gt;
        &lt;C&gt;
           &lt;C1&gt;
              &lt;C11&gt;C11&lt;/C11&gt;
              &lt;C12&gt;C12&lt;/C12&gt;
           &lt;/C1&gt;
           &lt;C2&gt;
              &lt;C21&gt;C21&lt;/C21&gt;
              &lt;C22&gt;C22&lt;/C22&gt;
           &lt;/C2&gt;
           &lt;C3&gt;
              &lt;C3&gt;C3&lt;/C3&gt;
           &lt;/C3&gt;
           &lt;D&gt;D&lt;/D&gt;
        &lt;/C&gt;
     &lt;/B&gt;
  &lt;/A&gt;
  </code></pre>
  
  <p>Below Output code generated:</p>
  
  <pre><code>&lt;A&gt;
    &lt;B&gt;
      &lt;C&gt;
        &lt;D&gt;
          &lt;C1&gt;
            &lt;C11&gt;
              &lt;C12&gt;
                &lt;C2&gt;
                  &lt;C21&gt;
                    &lt;C22&gt;
                      &lt;C3&gt;
                        &lt;C31/&gt;
                      &lt;/C3&gt;
                    &lt;/C22&gt;
                  &lt;/C21&gt;
                &lt;/C2&gt;
              &lt;/C12&gt;
            &lt;/C11&gt;
          &lt;/C1&gt;
        &lt;/D&gt;
      &lt;/C&gt;
    &lt;/B&gt;
  &lt;/A&gt;
  </code></pre>
  
  <p>Code(dirty) which I wrote:</p>
  
  <pre><code>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&lt;String, String&gt; data = new HashMap&lt;String, String&gt;();
              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&lt;String, String&gt;(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()&gt;0)){
                      mainRoot = document.createElement(buddyXML[0]);
                      document.appendChild(mainRoot);
                      System.out.println("root created\n");
                  }
  
                  for(int i=0; i&lt;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()&gt;0)){
                          System.out.println("creating Node "+buddyXML[i] + " &amp; 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 to 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;
      }
  }
  </code></pre>
  
  <p>This is not working as expected, I tried to change to some other appraoch but doesn't seems to work, could someone guide me on the same.</p>
  
  <p>Note: the data in the HashMap may incease horizontally and vertically, means, at run time I can have any number of node, so the requirement is to create a generic code.Prefered to use DomParser.</p>