RemoveChild 删除具有此名称的第一个 child,但跳过下一个具有相同名称的

RemoveChild removes the first child with this name, but skips the next one with the same name

我有这个节点

<Record status="updated">
    <ID_Country>5</ID_Country>
    <ID_Currency>162</ID_Currency>
    <IsoCodeNumber>16  </IsoCodeNumber>
    <IsoCodeLetter version="old">AS  </IsoCodeLetter>
    <IsoCodeLetter version="new">ASAS  </IsoCodeLetter>
    <PostCode>    </PostCode>
    <CountryName>American Samoa  </CountryName>                        
    <isEuCountry>0</isEuCountry>
</Record>

我正在尝试将此节点添加到另一个 XML-file 中并使其看起来像这样。

<Record>
     <ID_Country>5</ID_Country>
     <ID_Currency>162</ID_Currency>
     <IsoCodeNumber>16  </IsoCodeNumber>         
     <IsoCodeLetter>ASAS  </IsoCodeLetter>
     <PostCode>    </PostCode>
     <CountryName>American Samoa  </CountryName>                        
     <isEuCountry>0</isEuCountry>
</Record>

这是我使用的代码

Node updatedNode = diffNode.cloneNode(true);
((Element) updatedNode).removeAttribute("status");    

for (int i = 0; i < updatedNode.getChildNodes().getLength(); i++)
{
    if (updatedNode.getChildNodes().item(i).getNodeType() == Node.ELEMENT_NODE)
    {                           
         Element e = (Element)updatedNode.getChildNodes().item(i);
         String string = e.getNodeName();

         if (e.hasAttribute("version") && e.getAttribute("version").equals("old"))
         {                 
              ((Element) updatedNode).removeChild((Node)e);                                
         }
         if(e.hasAttribute("version") && e.getAttribute("version").equals("new"))
         {              
               e.removeAttribute("version");
         }
    }
} 
productXML.adoptNode(updatedNode);
prodRoot.insertBefore(updatedNode, nextNode);

出于某种原因,当循环经过第一个 IsoCodeLetter 节点并将其删除时,它会跳过下一个并转到 PostCode,但第二个 IsoCodeLetter 仍在新节点中,我将其附加到 XML-file 看起来像这样。

<Record>
      <ID_Country>5</ID_Country>
      <ID_Currency>162</ID_Currency>
      <IsoCodeNumber>16  </IsoCodeNumber>         
      <IsoCodeLetter version="new">ASAS  </IsoCodeLetter>
      <PostCode>    </PostCode>
      <CountryName>American Samoa  </CountryName>                        
      <isEuCountry>0</isEuCountry>
</Record>

您知道为什么会发生这种情况以及如何解决吗? 我正在使用 DOMParser 编写 XML 文件。

通过从元素中删除 child,您将从 child 节点中删除它,这意味着下一个 child 现在位于 [=52] 的索引处=] 你删除了。但是您的循环继续使用 next 索引,跳过 child。例如:假设你有:

0: Child A
1: Child B
2: Child C
3: Child D

对于 i == 0,假设您不删除 Child A。您的循环将 i 加一,然后您继续 i == 1。你删除Child B。现在你有:

0: Child A
1: Child C
2: Child D

..但是您的循环将一个添加到 i,现在 i == 2,您看到的下一个 child 是 Child D。您从未看过Child C.

通常的解决方案是:

  1. 向后循环,因此索引在递减,因此是否删除给定索引处的 child 并不重要;或

  2. 在修改元素之前拍摄 child 节点列表的 快照 ,这样当您从元素,它不会从您的快照中删除

后退是一个很容易的改变:

for (int i = updatedNode.getChildNodes().getLength() - 1; i >= 0; i--)

旁注:我可能还会只调用 getChildNodes 一次 并重复使用结果 NodeList object:

NodeList children = updatedNode.getChildNodes();
for (int i = children.getLength() - 1; i >= 0; i--)
    // ...and use `children`, not `updatedNode.getChildNodes()`, in the loop body...
}