将org.w3c.dom.Node转换为String

时间:2022-10-30 13:44:36

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 object into a string. I am using org.ccil.cowan.tagsoup Parser for my purpose.

对不起,我是一个Java / XML新手 - 似乎无法想出这个。似乎可以将Document对象转换为字符串。但是,我想将Node对象转换为字符串。我正在使用org.ccil.cowan.tagsoup Parser。

I'm retrieving the Node by something like...

我正在通过类似的方式检索节点...

 parser = new org.ccil.cowan.tagsoup.Parser() 

 parser.setFeature(namespaceaware, false)

 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 
 DOMResult domResult = new DOMResult(); 

 transformer.transform(new SAXSource(parser, new InputSource(in)), domResult);
 Node n = domResult.getNode();      

 // I'm interested in the first child, so...
 Node myNode = n.getChildNodes().item(0);

 // convert myNode to string..
 // what to do here?

The answer may be obvious, but I can't seem to figure out from the core Java libraries how to achieve this. Any help is much appreciated!

答案可能很明显,但我似乎无法从核心Java库中弄清楚如何实现这一目标。任何帮助深表感谢!

2 个解决方案

#1


44  

You can use a Transformer (error handling and optional factory configuration omitted for clarity):

您可以使用Transformer(为清晰起见,省略了错误处理和可选的工厂配置):

Node node = ...;
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(writer));
String xml = writer.toString();
// Use xml ...

#2


4  

String getNodeString(Node node) {
    try {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.toString();
        return output.substring(output.indexOf("?>") + 2);//remove <?xml version="1.0" encoding="UTF-8"?>
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return node.getTextContent();
}

#1


44  

You can use a Transformer (error handling and optional factory configuration omitted for clarity):

您可以使用Transformer(为清晰起见,省略了错误处理和可选的工厂配置):

Node node = ...;
StringWriter writer = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(writer));
String xml = writer.toString();
// Use xml ...

#2


4  

String getNodeString(Node node) {
    try {
        StringWriter writer = new StringWriter();
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        String output = writer.toString();
        return output.substring(output.indexOf("?>") + 2);//remove <?xml version="1.0" encoding="UTF-8"?>
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return node.getTextContent();
}