【发布时间】:2011-07-21 00:59:32
【问题描述】:
我的目标是用标签<something style="background-color:red"></something> 包装当前org.w3c.dom.Document 上的每一个dom 元素(Node.ELEMENT_NODE)。
public static void main(String[] args){
org.w3c.dom.DOMDocument doc;
paintAllNodes(doc, 0);
}
public static void paintAllNodes(Node node, int level) {
// Process node
// If there are any children, visit each one
NodeList list = node.getChildNodes();
for (int i=0; i<list.getLength(); i++) {
// Get child node
Node childNode = list.item(i);
// Visit child node
paintAllNodes(childNode, level+1);
}
}
【问题讨论】:
-
抱歉,您是要将属性
style添加到所有元素还是更改元素的名称? -
我想将
<something style="background-color:red'"></something>包裹在所有元素周围。 -
好问题,+1。有关此问题的最短和最简单的解决方案,请参阅我的答案。请记住将 XSLT 作为任何 XML 转换的 工具添加到您的工具集中。 :)
-
为什么不将您的赏金奖励给您最喜欢的答案?有什么理由吗? :-)