2016-07-12 3 views
0

안녕하세요 XML 노드를 삭제하는 데 문제가 있습니다.XML 하위 노드 삭제

<Instance> 
     <Internal> 
     <Attribute> 
     <Name>length</Name> 
     </Attribute> 
     <Name>internal</Name> 
     </Internal> 
    <Name>Instanec</Name> 
    </Instance> 

내 출력이 아래에 있도록 이름 노드를 삭제하고 싶습니다.

<Instance> 
     <Internal> 
     <Attribute> 
     </Attribute> 
     </Internal> 
    </Instance> 

나는 다음과 같은 코드를 시도 :

NodeList baseElmntLst2 = doc.getElementsByTagName("Name"); 
     for (int k = 0; k < baseElmntLst2.getLength(); k++) { 
      Element node = (Element) baseElmntLst2.item(k); 
      Element node2 = (Element) baseElmntLst2.item(k).getParentNode(); 
      node2.removeChild(node); 

}

을하지만 내가 이해 해달라고 모든 이름 요소를 삭제하지 않습니다.

감사합니다

+0

무엇을 사용하고 계십니까? – felipsmartins

답변

1

DOM NodeListlive collections 그래서 당신이 NodeList 한 가지 방식으로 모든 항목을 삭제하려면, 예를 들어 말에 시작하는 것입니다 for (int k = baseElmntLst2.getLength(); k >= 0; k--). 또는 while (baseElmntLst2.getLength() > 0) baseElmntLst2.item(0).getParentNode().removeChild(baseElmntLst2.item(0));을 사용하십시오.

+0

고맙습니다 완벽하게 작동합니다. –