Using: javax.xml and org.w3c:
public void removeNodeFromXML(File xmlfile_, String uuid)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(xmlfile_);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer tFormer = tFactory.newTransformer();
//????
Element rootElement = doc.getRootElement();
rootElement.removeChild("1236");
//???
// Normalize the DOM tree to combine all adjacent nodes
doc.normalize();
Source source = new DOMSource(doc);
Result dest = new StreamResult(xmlfile_);
tFormer.transform(source, dest);
}
XML looks like this
<Servers>
//remove this guy
<server ID="1236">
<name>Josh</name>
<port>1234</port>
<ip>12.2.2.3</ip>
</server>
<server ID="1237">
<name>John</name>
<port>1234</port>
<ip>12.2.2.3</ip>
</server>
</Servers>
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']