Java:如何用<;包装所有元素;sometag>;在org.w3c.dom中?

Java:如何用<;包装所有元素;sometag>;在org.w3c.dom中?,java,xml,xslt,dom,xpath,Java,Xml,Xslt,Dom,Xpath,我的目标是在当前org.w3c.dom.Document上用标记包装每个dom元素(Node.ELEMENT\u Node) publicstaticvoidmain(字符串[]args){ org.w3c.dom.dom文档; paintAllNodes(doc,0); } 公共静态void paintAllNodes(节点节点,int级别){ //进程节点 //如果有孩子,就去拜访每个孩子 NodeList list=node.getChildNodes(); 对于(inti=0;i,对于任

我的目标是在当前
org.w3c.dom.Document
上用标记
包装每个dom元素(
Node.ELEMENT\u Node

publicstaticvoidmain(字符串[]args){
org.w3c.dom.dom文档;
paintAllNodes(doc,0);
}
公共静态void paintAllNodes(节点节点,int级别){
//进程节点
//如果有孩子,就去拜访每个孩子
NodeList list=node.getChildNodes();

对于(inti=0;i,对于任何给定的节点“node”,以及文档“document”,它来自这样的东西应该可以工作

Node parent = node.getParent();
Node nextSibling = node.getNextSibling();

parent.removeChild(node);

Element something = document.createElement("something");
NamedNodeMap atts = something.getAttributes();
Attr att = document.createAttribute("style");
att.setNodeValue("background-color:red;");
atts.setNamedItem(att);
something.addChild(node);
parent.insertBefore(something, nextSibling);

这将在
org.w3c.dom.Document
中用
标记将每个
节点.ELEMENT\u节点
包装起来:

public static void paintAllNodes(Document doc) {
    // build list of Node.ELEMENT_NODE to process
    List<Node> nodes = new ArrayList<Node>();
    NodeList list = doc.getElementsByTagName("*");
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            nodes.add(node);
        }
    }

    // iterate through each node and wrap with <something> tag
    for (Node node : nodes) {
        // remember the next sibling for inserting at end
        Node nextSibling = node.getNextSibling();

        // remember the parent and remove this node from it
        Node parent = node.getParentNode();
        parent.removeChild(node);

        // create <something> element and attach node
        Element element = doc.createElement("something");
        NamedNodeMap attributes = element.getAttributes();
        Attr attr = doc.createAttribute("style");
        attr.setNodeValue("background-color:red");
        attributes.setNamedItem(attr);
        element.appendChild(node);

        // insert new element where the node was 
        parent.insertBefore(element, nextSibling);
    }
}
publicstaticvoidpaintallnodes(文档文档){
//生成要处理的Node.ELEMENT\u节点的列表
列表节点=新的ArrayList();
节点列表=doc.getElementsByTagName(“*”);
对于(int i=0;i

如果要排除任何节点,则只需在第一个
for
循环中过滤掉它们。

我想向您提出一个递归解决方案,它使用
节点#replaceChild
方法用新标记替换节点:

public static void paintAllNodes(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element somethingElement = node.getOwnerDocument().createElement("something");
        somethingElement.setAttribute("style", "background-color:red");
        node.getParentNode().replaceChild(somethingElement, node);
        somethingElement.appendChild(node);
        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            paintAllNodes(nodeList.item(i));
        }
    }
}
我用以下xml对其进行了测试:

<html>
    <head>
        <title>title</title>
    </head>
    <body>
    <h1>title</h1>
    <div>test</div>
    </body>
</html>

标题
标题
测试
My main已经打印出了这个新的xml,这似乎正是您想要的:

<?xml version="1.0" encoding="UTF-8"?><something style="background-color:red"><html>
    <something style="background-color:red"><head>
        <something style="background-color:red"><title>title</title></something>
    </head></something>
    <something style="background-color:red"><body>
    <something style="background-color:red"><h1>title</h1></something>
    <something style="background-color:red"><div>test</div></something>
    </body></something>
</html></something>

标题
标题
测试

希望这能有所帮助。

解决此类问题(与任何XML转换一样)的最简单方法之一是使用XSLT

此XSLT转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="*">
 <something style="background-color:red">
   <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="node()"/>
   </xsl:copy>
  </something>
 </xsl:template>
</xsl:stylesheet>
<something style="background-color:red">
   <nums>
      <something style="background-color:red">
         <num>01</num>
      </something>
      <something style="background-color:red">
         <num>02</num>
      </something>
      <something style="background-color:red">
         <num>03</num>
      </something>
      <something style="background-color:red">
         <num>04</num>
      </something>
      <something style="background-color:red">
         <num>05</num>
      </something>
      <something style="background-color:red">
         <num>06</num>
      </something>
      <something style="background-color:red">
         <num>07</num>
      </something>
      <something style="background-color:red">
         <num>08</num>
      </something>
      <something style="background-color:red">
         <num>09</num>
      </something>
      <something style="background-color:red">
         <num>10</num>
      </something>
   </nums>
</something>

应用于任何XML文档时,例如此文档:

<nums>
  <num>01</num>
  <num>02</num>
  <num>03</num>
  <num>04</num>
  <num>05</num>
  <num>06</num>
  <num>07</num>
  <num>08</num>
  <num>09</num>
  <num>10</num>
</nums>

01
02
03
04
05
06
07
08
09
10
生成所需的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="*">
 <something style="background-color:red">
   <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates select="node()"/>
   </xsl:copy>
  </something>
 </xsl:template>
</xsl:stylesheet>
<something style="background-color:red">
   <nums>
      <something style="background-color:red">
         <num>01</num>
      </something>
      <something style="background-color:red">
         <num>02</num>
      </something>
      <something style="background-color:red">
         <num>03</num>
      </something>
      <something style="background-color:red">
         <num>04</num>
      </something>
      <something style="background-color:red">
         <num>05</num>
      </something>
      <something style="background-color:red">
         <num>06</num>
      </something>
      <something style="background-color:red">
         <num>07</num>
      </something>
      <something style="background-color:red">
         <num>08</num>
      </something>
      <something style="background-color:red">
         <num>09</num>
      </something>
      <something style="background-color:red">
         <num>10</num>
      </something>
   </nums>
</something>

01
02
03
04
05
06
07
08
09
10
注意:如何在Java代码中启动XSLT转换留给读者作为练习:)

作为非答案;-)我建议使用CSS…
一些东西{background:red}

显然,这只有在使用CSS的情况下才有意义。

对不起,您是想向所有元素添加属性
style
,还是想更改元素的名称?我想将
包装在所有元素周围。好问题,+1。请参阅我的答案,以获得此问题的最短和最简单的解决方案。请记住将XSLT添加到y中我们的工具集可作为任何XML转换的工具。:)为什么不奖励您最喜欢的答案?有什么原因吗?:-)这只会返回一个空白文档。它会清除加载的DOM文档。您能给出一个您正在处理的文档的示例吗?我用它进行了测试,上面的代码运行良好:
testheaderTest body

+1用于使用
doc.getElementsByTagName(“*”)
避免递归。我已经测试过了,它是有效的!我还添加了我的答案和一个替代解决方案。我在`Element somethingElement=node.getOwnerDocument().createElement(“某物”)处得到nullexception
。你做到了吗?我已经更新了这个问题。我使用了你的代码。我编辑了我的答案,我更改了几行代码以使其更可靠。
getOwnerDocument()
方法如果节点是文档或文档类型,则返回null。现在您还可以将整个
文档
传递给
paintAllNodes
方法。您能试试吗?是的,我已经运行了新代码,但它似乎不起作用。我尝试在for括号内打印一些文本,但没有打印出来。@Alejandro:谢谢,如果XSLT的答案能得到这个问题的赏金那就太好了——有些人需要了解哪种工具是转换XML的最佳工具:)