public boolean editXML(String newCar) {
try {
String myXMLfile = "res\\values\\cars.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(myXMLfile);
Node string_array = doc.getElementsByTagName("string-array")
.item(0);
Element item = doc.createElement("item");
item.setTextContent(newCar);
string_array.appendChild(item);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(myXMLfile));
transformer.transform(source, result);
return true;
} catch (Exception e) {
return false;
}
}
如果我运行一个普通的 JAVA 项目,上面的函数可以正常工作,但是,如果我在一个 android 项目中运行它,它会返回 false。问题出在哪里?如何在 android 项目中预览 e.printstacktrace 以便我可以看到异常的内容?