Reputation: 1069
I have posted a question with similar name. I tried to delete it, but since it has answer, I am not able to. However, question I have now is different, as I got stucked with the entire method. I apologise for possible inconveince and hope you'll still help me out.
THIS IS MY CODE:
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();
}
EXPLANATION: fetchXML()
is inside HandleXML class
(HandleXML.java), so it is not in MainActivity
, which I figured could be important, while copy / paste codes on how to Save Files from Web ( watched tutorials). When I seem to implement codes like:
COPIED FROM ANDROID TUTORIAL PAGE:
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
I receive errors with openFileOutput
.
QUESTION: How can I save the InputStream
I get from fetchXML()
the easiest way ? Code snippet would really be a savour.
Hope someone can help. Thanks.
Upvotes: 0
Views: 2481
Reputation: 3404
Exmaple for creating DOM xml file in internal storage:
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());
}
Upvotes: 1
Reputation: 3993
Try using this method:
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);
}
Upvotes: 0