标签:parse lock jaxp factor 回写 source err nsf except
/
修改第一个p1下面的sex内容是nan
* 1.创建解析器工厂
* 2.根据解析器工厂创建解析器
* 3.解析xml返回document
* 4.得到sex item方法
* 5.修改sex里面的值 setTextContent方法
*
* 6.回写xml
* */
private static void changeNode() 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 sex1 = document.getElementsByTagName("sex").item(0);
//修改sex值
sex1.setTextContent("nan");
//回写xml
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(document), new StreamResult("src/person.xml"));
}
标签:parse lock jaxp factor 回写 source err nsf except
原文地址:https://www.cnblogs.com/selfdef/p/11093658.html