How do I remove a node element by id in XML?

后端 未结 2 1788
醉酒成梦
醉酒成梦 2021-01-21 11:12

Using: javax.xml and org.w3c:

public void removeNodeFromXML(File xmlfile_, String uuid)
  {
    DocumentBuilderFactory factory = Docume         


        
相关标签:
2条回答
  • 2021-01-21 11:50

    You can use XPath to select specific elements/attributes. Just search the web for Tutorials. Here is good one. You should also read the Java-Doc for java.xml.xpath, which includes short examples.

    The XPath-Expression for your XML-File is: /server[@ID='xxxx']

    0 讨论(0)
  • 2021-01-21 11:51

    You can use:

    Element element = doc.getElementById("1236");
    element.getParentNode().removeChild(element);
    

    This should give you the element with ID "1236". You then get the parent node for the element and remove the element by passing the element with ID "1236" to removeChild.

    See here for a full example.

    Hope this helps.

    0 讨论(0)
提交回复
热议问题