[英]How to delete node from xml based on attribute using java with DOM Parser
I want to delete a node from XML file based on attribute using JAVA with DOM Parser. 我想使用具有DOM Parser的JAVA从基于属性的XML文件中删除节点。 For Example 例如
<company>
<staff id="1">
<firstname>yong</firstname>
</staff>
<staff id="2">
<firstname>low</firstname>
</staff>
</company>
Based on staff id,i want to delete node. 基于人员ID,我想删除节点。
Thanks for reply. 谢谢您的回复。 Above solution which i posted is correct.Only i need to write xml again with some transform API. 以上我发布的解决方案是正确的。只有我需要使用一些转换API再次编写xml。 Posting solution,if any body has the same problem. 发布解决方案,如果任何机构有相同的问题。
try{
//Save the Created XML on Local Disc using Transformation APIs as Discussed
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source s = new DOMSource(doc);
Result res = new StreamResult( new FileOutputStream(fXmlFile));
try {
transformer.transform(s, res);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch(TransformerConfigurationException e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.