加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java:如何在org.w3c.dom中包装所有元素?

发布时间:2020-12-15 05:22:59 所属栏目:Java 来源:网络整理
导读:我的目标是将每个dom元素(Node.ELEMENT_NODE)包装在当前org.w3c.dom.Document上,标签为 something style =“background-color:red” / something. public static void main(String[] args){ org.w3c.dom.DOMDocument doc; paintAllNodes(doc,0);}public sta
我的目标是将每个dom元素(Node.ELEMENT_NODE)包装在当前org.w3c.dom.Document上,标签为< something style =“background-color:red”>< / something>.

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);
    }
}

解决方法

我想建议一个递归解决方案,它利用Node#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));
        }
    }
}

这是我的主要内容:

public static void main(String[] args) throws SAXException,IOException,ParserConfigurationException,TransformerException {

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new File("document.xml"));
    paintAllNodes(document.getDocumentElement());

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);
    transformer.transform(source,result);
}

我用这个xml测试了它:

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

我的主要打印出这个新的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>

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读