我试图解决我的xml问题,但我被卡住了...我在控制台上将xml作为System.Out
得到了很好的解决方案。但是,当我试图将它作为返回字符串值时,它只给我一半的xml(返回的字符串不包含错误,但它已经破坏了xml)。代码如下。 (ide:androidstudio,尝试过jdk 1.7 / 1.8相同的结果)
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
// Output to console for testing
StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(source, consoleResult); // this give all data to as System.Out as correct every line
xmlString = result.getWriter().toString();
Log.v("XML OUTPUT xmlString", xmlString.toString()); // but this gives only part of xml like broken from half part...
更新: 解决方案,没有错,但Log.v剪切字符串......这意味着代码正常工作。感谢给@wero提示