如何删除xml中的特定节点?

时间:2012-06-08 13:26:22

标签: java xml xmlnode xmlnodelist

我有一个问题是删除xml中的节点,我有节点列表我存储了一些细节,现在我搜索了一个节点值(somevalue),我想删除相应的子节点 这是xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Serverinformation>
<server>
<serverip>172.1.2.111</serverip>
<port>4000</port>
<dbip>162.2.1.111</dbip>
<dbname>sample</dbname>
<nor>1000</nor>
</server>
<server>
<serverip>182.1.2.111</serverip>
<port>6000</port>
<dbip>152.2.1.111</dbip>
<dbname>sample2</dbname>
<nor>1000</nor>
</server>
</Serverinformation>

//

 Element e = doc.getDocumentElement();
 NodeList nodeList = doc.getElementsByTagName("server");
 for (int i = 0; i < nodeList.getLength(); i++) {
   Node node = nodeList.item(i);
   if (node.getNodeType() == Node.ELEMENT_NODE) {
         Element element = (Element) node;
         NodeList nodelist = element.getElementsByTagName("serverip");
                Element element1 = (Element) nodelist.item(0);
                NodeList fstNm = element1.getChildNodes();
                String str=(fstNm.item(0).getNodeValue());
                String t="172.1.2.111";
                    if(str.equals(t))
                        { 
             element1.getParentNode().removeChild(element1);//here is the problem

          count=1;
          break
                        }               
              continue;
                }
          } 
        if(count==1)
        System.out.println("item found");
        else
        System.out.println("item not found");
    }
}

所以现在我要删除那个具有该值“172.1.2.111”的节点;这段代码出了什么问题

0 个答案:

没有答案