Java编程jaxp中的dom解析器,能够直接修改xml文件中的标签名吗?
好像没有setNodeName()之类的方法?
其他的解析器能不能修改标签名?sax?dom4j?
好像没有setNodeName()之类的方法?
其他的解析器能不能修改标签名?sax?dom4j?
解决方案
10
可以使用dom4j先remove掉然后再重新添加。
http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/index.html
http://blog.csdn.net/yapingge2014/article/details/38322113
http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/index.html
http://blog.csdn.net/yapingge2014/article/details/38322113
30
DOM4J写的:
SAXReader sreader = new SAXReader();
try {
Document document = sreader.read(Thread.currentThread()
.getContextClassLoader().getResourceAsStream("test.xml"));
Element root = document.getRootElement();
List cars = root.elements();
for(Iterator iter=cars.iterator(); iter.hasNext();) {
Element car = (Element)iter.next();
car.setName("car");
System.out.println(car.getName());
}
FileOutputStream fos = new FileOutputStream("src/test.xml");
XMLWriter writer = new XMLWriter(fos);
writer.write(document);
writer.flush();
writer.close();
System.out.println("end");
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}