Converting a org.w3c.dom.Document in Java to String using Transformer

我的未来我决定 提交于 2019-12-11 18:37:51

问题


I'm trying to convert a XML org.w3c.dom.Document to a String using a Transformer:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {

        e.printStackTrace();
    }

    Document doc = builder.newDocument();

    Element gameNode = doc.createElement("Game");
    gameNode.setAttribute("gameID", String.valueOf(game.getGameID()));
    gameNode.setAttribute("username", game.getUsername());
    gameNode.setAttribute("gameStartTime", String.valueOf(game.getGameStartTime()));
    gameNode.setAttribute("gameStartDate", String.valueOf(game.getGameStartDate()));
    gameNode.setAttribute("totalScore", String.valueOf(game.getTotalScore()));
    gameNode.setAttribute("totalTimeInSeconds", String.valueOf(game.getTotalTimeInSeconds()));
    gameNode.setAttribute("lastLevel", String.valueOf(game.getLastLevel()));
    gameNode.setAttribute("firstLevel", String.valueOf(game.getFirstLevel()));

    doc.appendChild(gameNode);



    for(int i = 0 ; i < game.getScores().size(); i++)
    {
        Element scoreNode = doc.createElement("Score");
        scoreNode.setAttribute("gameID", String.valueOf(game.getScores().get(i).getGameID()));
        scoreNode.setAttribute("points", String.valueOf(game.getScores().get(i).getPoints()));
        scoreNode.setAttribute("levelOfScore", String.valueOf(game.getScores().get(i).getLevelOfScore()));
        scoreNode.setAttribute("timeToScore", String.valueOf(game.getScores().get(i).getTimeToScore()));

        gameNode.appendChild(scoreNode);
    }


    return DocumentToString(doc);

I'm using my objects called Game and Score, which shouldn't interfere with the process.

public static String DocumentToString(Document doc)
{
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(System.out);  
        transformer.transform(source, result); 
        return "yeah";
    } catch (Exception ex) {
        return "Error converting to String";
    }

}

For some reason the transformer.transform method does not complete successfully.


回答1:


Here is Document converter which works fine for me

public String getStringFromDoc(Document doc) throws TransformerException {

        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        writer.flush();
        return writer.toString();
    }


来源:https://stackoverflow.com/questions/22539158/converting-a-org-w3c-dom-document-in-java-to-string-using-transformer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!