Java 如何在Android内部存储器中保存XML文件
我发布了一个同名的问题。我试图删除它,但由于它有答案,我无法删除。然而,我现在的问题是不同的,因为我被整个方法绊住了。我为可能的疏忽道歉,希望你仍能帮助我 这是我的代码:Java 如何在Android内部存储器中保存XML文件,java,android,xml,android-activity,Java,Android,Xml,Android Activity,我发布了一个同名的问题。我试图删除它,但由于它有答案,我无法删除。然而,我现在的问题是不同的,因为我被整个方法绊住了。我为可能的疏忽道歉,希望你仍能帮助我 这是我的代码: public void fetchXML(){ Thread thread = new Thread(new Runnable(){ @Override public void run() { try { URL url = ne
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)
url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES
, false);
myparser.setInput(is, null);
parseXMLAndStoreIt(myparser);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
说明:fetchXML()
在HandleXML类(HandleXML.java)中,因此它不在MainActivity
中,我认为这可能很重要,而复制/粘贴有关如何从Web保存文件的代码(观看的教程)。当我似乎在执行以下代码时:
从ANDROID教程页面复制:
我收到openFileOutput的错误
问题:如何保存从fetchXML()获取的InputStream
最简单的方法?代码片段真的是一种享受
希望有人能帮忙。谢谢。尝试使用以下方法:
public void ConvertingAnInputStreamToAFile(InputStream inputStream) throws IOException {
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
File targetFile = new File("targetFile.xml"); //<-- Your File Location here
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
}
public void将NinputStream转换为文件(InputStream InputStream)引发IOException{
字节[]缓冲区=新字节[inputStream.available()];
inputStream.read(缓冲区);
File targetFile=new File(“targetFile.xml”);//Exmaple用于在内部存储器中创建DOM xml文件:
public void CreateXmlFile(){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
builder = dbf.newDocumentBuilder();
Document doc = builder.newDocument();
// root element
org.w3c.dom.Element root = doc.createElement("mobile-app");
doc.appendChild(root);
//create a comment
Comment comment=doc.createComment("This is comment");
//add in the root element
root.appendChild(comment);
prettyPrint(doc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public final void prettyPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(getFilesDir().getAbsolutePath() + "/newXmlFile.xml"));
System.out.println(out.toString());
}
你把这个“保存文件”的代码放在哪里了?放在你的Handle XML类中了?也总是发布你的日志。嘿,你试过我的答案了吗?
public void CreateXmlFile(){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
builder = dbf.newDocumentBuilder();
Document doc = builder.newDocument();
// root element
org.w3c.dom.Element root = doc.createElement("mobile-app");
doc.appendChild(root);
//create a comment
Comment comment=doc.createComment("This is comment");
//add in the root element
root.appendChild(comment);
prettyPrint(doc);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public final void prettyPrint(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
Writer out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(getFilesDir().getAbsolutePath() + "/newXmlFile.xml"));
System.out.println(out.toString());
}