如何从xml java中删除元素

如何从xml java中删除元素,java,xml,Java,Xml,我试图从xml中删除某个estrofe元素,但是如果该元素没有子元素,我如何删除该元素,因为唯一的删除函数是removeChild()。谢谢你的帮助 public boolean remove(short numEstrofe) { //System.out.println("Falta implementar!"); NodeList list = D.getElementsByTagName("estrofe"); for

我试图从xml中删除某个estrofe元素,但是如果该元素没有子元素,我如何删除该元素,因为唯一的删除函数是removeChild()。谢谢你的帮助

public boolean remove(short numEstrofe) {
    //System.out.println("Falta implementar!");
     NodeList list = D.getElementsByTagName("estrofe");

     for (int i = 0; i < list.getLength(); i++) {
         if (i == numEstrofe) {
             Element estrofe = (Element) list.item(i);
             
             D.removeChild(estrofe);
             return true;
         }
     }
    return false;
}~
public boolean remove(短numEstrofe){
//System.out.println(“Falta implementar!”);
节点列表=D.getElementsByTagName(“estrofe”);
对于(int i=0;i
这是xml

<poema>
<autor>Luís de Camões</autor>
<título>Mudam-se os tempos, mudam-se as vontades</título>
<estrofe>
    <verso>Mudam-se os tempos, mudam-se as vontades,</verso>
    <verso>Muda-se o ser, muda-se a confiança;</verso>
    <verso>Todo o mundo é composto de mudança,</verso>
    <verso>Tomando sempre novas qualidades.</verso>
</estrofe>
<estrofe>
    <verso>Continuamente vemos novidades,</verso>
    <verso>Diferentes em tudo da esperança;</verso>
    <verso>Do mal ficam as mágoas na lembrança,</verso>
    <verso>E do bem, se algum houve, as saudades.</verso>
</estrofe>
<estrofe>
    <verso>O tempo cobre o chão de verde manto,</verso>
    <verso>Que já coberto foi de neve fria,</verso>
    <verso>E em mim converte em choro o doce canto.</verso>
</estrofe>
<estrofe>
    <verso>E, afora este mudar-se cada dia,</verso>
    <verso>Outra mudança faz de mor espanto:</verso>
    <verso>Que não se muda já como soía.</verso>
</estrofe>
</poema>

卢伊斯·德坎斯
以节奏为节奏,以vontades为节奏
以节奏为节奏,以vontades为中心,
这是一个好消息,一个好消息;
托多·蒙多·孔波斯托·德·穆丹萨,
托曼多·塞姆普雷·诺瓦斯是合格的。
新维莫斯酒店,
不同的世界语;
马尔·菲卡姆扮演的是马戈斯·纳莱姆布拉萨,
我是索达德人,我是索达德人。
快,快,快,快,
我的朋友们,
E em mim转换为em choro doce canto。
E、 阿福拉埃斯特穆达东南卡达迪亚,
奥特拉·穆丹萨·法兹·德莫埃斯潘托:
你的名字叫查科莫·索亚。

差不多了。不要从临时节点列表中删除该元素,而是从其父元素中删除它

Element estrofe = (Element) list.item(i);
estrofe.getParent().removeChild(estrofe);