What is the shortest way to pretty print a org.w3c.dom.Document to stdout?

前端 未结 6 1390
难免孤独
难免孤独 2020-11-27 11:08

What is the easiest way to pretty print (a.k.a. formatted) a org.w3c.dom.Document to stdout?

6条回答
  •  迷失自我
    2020-11-27 11:51

    private void printNode(Node rootNode, String spacer) {
        System.out.println(spacer + rootNode.getNodeName() + " -> " + rootNode.getNodeValue());
        NodeList nl = rootNode.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++)
            printNode(nl.item(i), spacer + "   ");
    }
    

提交回复
热议问题