解析后
拿子节点列表用Node自己的方法
removeChild(NodeList.item(i));一个一个弃掉。 Element rootEle = null;
DocumentBuilderFactory factory ;
DocumentBuilder builder;
InputSource inputSource ;
Document doc;
// 解析処理
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
inputSource = new InputSource(new FileInputStream(arg_objXMLFile));
doc = builder.parse(inputSource);
rootEle = doc.getDocumentElement();
rootEle.normalize();
}catch (Exception e){
拿子节点列表用Node自己的方法
removeChild(NodeList.item(i));一个一个弃掉。 Element rootEle = null;
DocumentBuilderFactory factory ;
DocumentBuilder builder;
InputSource inputSource ;
Document doc;
// 解析処理
try {
factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
inputSource = new InputSource(new FileInputStream(arg_objXMLFile));
doc = builder.parse(inputSource);
rootEle = doc.getDocumentElement();
rootEle.normalize();
}catch (Exception e){
for(int i=0;i<list.getlength();i++){
rootEle.removeChile(list.item(i));
}
这样就吧名字为customer的节点和子节点全删掉了。
//
NodeList nlID=doc.getElementsByTagName("cust-id");
//if you need find id=12001
for(int i=0;i<nlID.getLength().i++){
if(nlID.item(i).getNodeValue().equals("12001")){
doc.removeChild(nlID.item(i).getParent());
}
if(nlID.item(i).getNodeValue().equals("12001")){
//or try it;
nlID.item(i).getParentNode.getParentNode.removeChild(nlID.item(i).getParentNode());
}
我改为:
if(nlID.item(i).getFirstChild.getNodeValue().equals("12001"))
测试通过,
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import org.xml.sax.InputSource;
import java.io.FileInputStream;
import java.io.File;
...
File tmpfile = File.createTempFile("tmp","tmp");
String path = tmpfile.getPath();
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult rs = new StreamResult(new File(path));
tf.transform(source,rs);
...
改改看