标签:str builder new oms 工厂 orm name 创建 pat
/
删除sex结点
* 1.创建解析器工厂
* 2.根据解析器工厂创建解析器
* 3.解析xml返回document
*
* 4.得到sex结点
* 5.得到sex的父节点 getPatentNode
* 6.删除sex removeChild方法
*
* 7.回写xml
* */
private static void removeNode() throws ParserConfigurationException, SAXException, IOException,
TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
//创建解析器工厂
DocumentBuilderFactory builderFactoty = DocumentBuilderFactory.newInstance();
//创建解析器
DocumentBuilder builder = builderFactoty.newDocumentBuilder();
//解析xml返回document
Document document = builder.parse("src/person.xml");
//得到sex结点
Node sex = document.getElementsByTagName("sex").item(0);
//得到sex的父节点
Node sexParent = sex.getParentNode();
//删除sex
sexParent.removeChild(sex);
//回写xml
TransformerFactory transformFactory = TransformerFactory.newInstance();
Transformer transformer = transformFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/person.xml"));
}
标签:str builder new oms 工厂 orm name 创建 pat
原文地址:https://www.cnblogs.com/selfdef/p/11093664.html