简体   繁体   中英

Add and Remove Elements from XML - DOM Parser - Java

XML

<company> <employee>
<age> 12 </age>
  <name> name1</name>
</employee> 
 <employee>
 <age> 12 </age>
  <name> name1</name>
  <status>active</status>
</employee>

<employee>
 <age> 12 </age>
  <name> name1</name>
</employee></company>

Java Code:

employeeList nList = doc.getElementsByTagName("employee");
     for (int i = 0; i < nList.getLength(); i++) {
            Node employeeNode= nList.item(i);
            employeeList employeeList = nNode.getChildNodes();
            Node insertNode=null;
            System.out.println(" Processing the " + i + " Portlet Tag");
                //Inner Loop to Process each Portlet tags   
                 int employeeList_Count=employeeList.getLength();

                 for (int j = 0; j < employeeList_Count; j++) {
                      Node childNode = employeeList.item(j);


                      if( childNode.getNodeName()) == "status")  {
                               removeNode(nNode,childNode);      //   assume remove functionality perfectly works (actually it is!!)
                      }
                      if ( j == employeeList_Count - 2)  // goes into loop during last node
                      {

                          Element insertElement = (Element)nNode;

                          insertElement.insertBefore(employee_status_element,  insertElement.getFirstChild().getNextSibling());  //employee_status_element,   this is the element should be inserted in all employee tags


                           doc.getDocumentElement().normalize();
                           updateXml2File(doc, xmlDTDPath , outputFile);    // functionality to write the xml into file
                      }

                }



          }

Every time i run this code, only the last employee (out of 3) gets updated with element "status" ...

this is out the output is like

first loop completes tag is in the first employee tag .. it pushed to second during second loop. finally only last element has the status element.

really strage... really appreciate your guidance.

I have fixed the issue. It was silly but got stricked later after starrring the code for few hours.

Logic was like this before

Create Element
  Loop for Multiple Employees
        Attach/Append the Element to Employees

Issue: Element attached only to the last employee node in the loop.

Cause: Since I created only one Element before loop.. i cannot attach it to multiple Employee Nodes

Solution Worked:

Now i move the element creating into the loop then it started worked.

  Loop for Multiple Employees
        Create Element   
        Attach/Append the Element to Employees

My Question and code was little messy, i tried to correct it as much as possible to make it simple but not able to... thanks every one who tried to resolve this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM