0 Replies Latest reply: Dec 28, 2013 10:17 AM by Soumik RSS

    Adding child nodes to xml reccursively using java

    Soumik

      Hi,

       

      I am having input XML as below:

       

      <company name = "Company Name">
           <departments>
                <department id = "1" name = "Department 1 Name" childs = "true" />
               <department id = "2" name = "Department 2 Name" childs = "false" />
               <department id = "3" name = "Department 3 Name" childs = "true" />
           </departments>
      </company>
      
      
      
      

       

      And a Map containing the key as id of each department. Eg:

       

      Key = "1"

      Value =

      <departments>
           <department id = "4" name = "Department 4 Name" childs = "yes" />
            <department id = "6" name = "Department 6 Name" childs = "yes" />
      </departments>
      
      
      
      

       

      Key = "4"

       

      Value =

      <departments>
           <department id = "5" name = "Department 5 Name" childs = "no" /> 
      </departments>
      
      
      
      

       

      Key = "6"

      Value =

      <departments>
          <department id = "7" name = "Department 7 Name" childs = "yes" />
          <department id = "14" name = "Department 14 Name" childs = "false" />
          <department id = "9" name = "Department 9 Name" childs = "true" />
      </departments>
      
      
      

       

      Key = "7"

      Value =

      <departments>
          <department id = "8" name = "Department 8 Name" childs = "yes" />
          <department id = "10" name = "Department 10 Name" childs = "no" />
      </departments>
      
      
      

       

      And so on

       

      i.e. a List containing all the department id and corresponding sub departments

       

      Expected Response is:

      <company name = "Company Name">
          <departments>
              <department id = "1" name = "Department 1 Name" childs = "yes" >
                  <departments>
                      <department id = "4" name = "Department 4 Name" childs = "yes" >
                          <departments>
                              <department id = "5" name = "Department 5 Name" childs = "no" />
                          </departments>
                      </department>
                      <department id = "6" name = "Department 6 Name" childs = "yes" >
                          <departments>
                              <department id = "7" name = "Department 7 Name" childs = "yes" >
                                  <departments>
                                      <department id = "8" name = "Department 8 Name" childs = "yes" >
                                          <departments>
                                              <department id="12" name="Department 12 Name" childs="no"/>
                                              <department id="13" name="Department 13 Name" childs="no"/>
                                          </departments>
                                      </department>
                                      <department id = "10" name = "Department 10 Name" childs = "no" />
                                  </departments>
                              </department>
                              <department id = "14" name = "Department 14 Name" childs = "false" />
                              <department id = "9" name = "Department 9 Name" childs = "true" >
                                  <departments>
                                      <department id = "15" name = "Department 15 Name" childs = "false" />
                                  </departments>
                              </department>
                          </departments>
                      </department>
                  </departments>
              </department>
              <department id = "2" name = "Department 2 Name" childs = "false" />
              <department id = "3" name = "Department 3 Name" childs = "true" >
                  <departments>
                      <department id = "11" name = "Department 11 Name" childs = "false" />
                  </departments>
              </department>
          </departments>
      </company>
      
      
      
      

       

      Can someone please help in achieving the same using java

       

      Below is the java code used:

       

      public static Node getReferences(Map<String, Node> resources, String key) {
      Node result = null;
      try {
                XPath xPath = XPathFactory.newInstance().newXPath();
                XPathExpression xPathExpression = xPath.compile("//department[@childs='yes']");
                Node n1 = resources.get(key);
                if (n1.getNodeType() == Node.ELEMENT_NODE &&
                      n1.getNodeName().equalsIgnoreCase("departments")) {
                     NodeList nL1 = (NodeList)xPathExpression.evaluate(n1, XPathConstants.NODESET);
                     if (nL1.getLength() > 0) {
                          for (int i = 0; i < nL1.getLength(); i++) {
                               Node n2 = nL1.item(i);
                               Document document1 = n2.getOwnerDocument();
                              Element e1 = (Element)n2;
                               if (e1.getAttribute("childs").equalsIgnoreCase("yes")) {
                                    String serviceName = e1.getAttribute("id");
                                    Node n3 = resources.get(serviceName);
                                  Node n4 = getReferences(resources, serviceName);
                                    n3 = document1.importNode(n3, true);
                                    Node appendChild = n2.appendChild(n3);
                               }
                          }
                     } else {
                          result = n1;
                     }
                }
      } catch (XPathExpressionException e) {
                  e.printStackTrace();
           } catch (Exception e) {
                  e.printStackTrace();
           }
      return result;
      }