Java tutorial
//package com.java2s; //License from project: Apache License import java.io.FileInputStream; import java.util.Properties; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void removeHandset(String file, String name) throws Exception { DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); DocumentBuilder dombuilder = domfac.newDocumentBuilder(); FileInputStream is = new FileInputStream(file); Document doc = dombuilder.parse(is); NodeList devices = doc.getElementsByTagName("devices"); NodeList nodeList = doc.getElementsByTagName("device"); for (int i = 0; i < nodeList.getLength(); i++) { Node deviceNode = nodeList.item(i); if (deviceNode.getTextContent().equals(name)) { devices.item(0).removeChild(deviceNode); } } //save TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties props = t.getOutputProperties(); props.setProperty(OutputKeys.ENCODING, "GB2312"); t.setOutputProperties(props); DOMSource dom = new DOMSource(doc); StreamResult sr = new StreamResult(file); t.transform(dom, sr); } }