0
给定一个xml文件作为输入如何用新的字符串值修改标记的属性?更新xml标记的属性
功能是
updateXMLAttribute(Document doc , String tag, String attribute, String newValue){
//impl
}
我该怎么办呢?
给定一个xml文件作为输入如何用新的字符串值修改标记的属性?更新xml标记的属性
功能是
updateXMLAttribute(Document doc , String tag, String attribute, String newValue){
//impl
}
我该怎么办呢?
我认为,通过文档你的意思是org.w3c.dom.Document中:
updateXMLAttribute(Document doc , String tag, String attribute, String newValue) {
NodeList nodes = doc.getElementsByTagName(tag);
for(int i=0; i<nodes.getLength(); i++) {
if(nodes.item(i) instanceof Element) {
Element elem = (Element)nodes.item(i);
Attr attribute = elem.getAttributeNode(attribute);
attribute.setValue(newValue);
}
}
}
这将更新所有属性的DOM文档中命名的元素命名的属性的值。 当然,你应该添加适当的错误处理和空值检查。
PS:您可以在dom api文档中找到所有信息:http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Document.html