1 Reply Latest reply: Apr 3, 2014 3:34 PM by TPD-Opitz RSS

    How to add new sub-hierarchy in xml

    skytrace

      Could you please tell me what I do wrong

       

      I have next xml stucture:

       

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <services>
          <people>
              <name>Julia</name>
              <secondName>Roberts</secondName>
          </people>
      </services>
      
      
      
      
      
      

       

      So, I need to add new <people>...</people> stucture. I made next solution:

       

      NodeList employees = doc.getElementsByTagName("people");
              Element emp = null;
      
              for (int i = 0; i < employees.getLength(); i++) {
                  emp = (Element) employees.item(i);
                  Element people = doc.createElement("people");
      
                  Element name = doc.createElement("name");
                  name.appendChild(doc.createTextNode("Robert"));
                  emp.appendChild(people.appendChild(name));
             
                  Element lastName = doc.createElement("secondName");
                  lastName.appendChild(doc.createTextNode("Deniro"));
                  emp.appendChild(people.appendChild(lastName));
              }
      
      
      
      
      
      

       

      Then I got

       

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <services>
          <people>
              <name>Julia</name>
              <secondName>Roberts</secondName>
          </people>
          <name>Robert</name>
          <secondName>Deniro</secondName>
      </services>
      
      
      
      
      
      

       

      Yeas, I got the new person in my xml file, but it without

      <people>...</people>

       

      Could you please tell me where I did mistake in my algorithm?