本文介绍了使用Transformer将Java中的org.w3c.dom.Document转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Transformer将XML org.w3c.dom.Document转换为字符串:
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);
我正在使用名为Game and Score的对象,它不应干扰该过程。 / p>
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";
}
}
出于某种原因,transformer.transform方法未成功完成。
For some reason the transformer.transform method does not complete successfully.
推荐答案
这是文档转换器,对我来说很好用
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();
}
这篇关于使用Transformer将Java中的org.w3c.dom.Document转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!