Convert an org.w3c.dom.Node into a String

前端 未结 3 439
忘掉有多难
忘掉有多难 2020-12-09 15:25

Sorry I\'m a Java/XML newbie - and can\'t seem to figure this one out. It seems it\'s possible to convert a Document object to a string. However, I want to convert a Node ob

3条回答
  •  攒了一身酷
    2020-12-09 16:05

    This is way to convert Node to html

    public static String getInnerHTML(Node node) throws TransformerConfigurationException, TransformerException
    {
        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer proc = factory.newTransformer();
        proc.setOutputProperty(OutputKeys.METHOD, "html");
        for (int i = 0; i < node.getChildNodes().getLength(); i++)
        {
            proc.transform(new DOMSource(node.getChildNodes().item(i)), result);
        }
        return sw.toString();
    }
    

提交回复
热议问题