Saturday, May 11, 2013

[android help] XML file is created only once not every time when application is executed

android - XML file is created only once not every time when application is executed - Stack Overflow








Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















I'm trying to generate XML file on sdcard. I've added user permission in Manifest file but when I plug my phone though usb and run the application in eclipse then for the first time xml file gets created but when I again run application through phone only or through eclipse then it doesn't gets created. For the file to get created I've to reconnect my phone through usb and in that also it gets created only once. Please help me.


package com.example.samplexml;



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.Document;
import org.w3c.dom.Element;


import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
TextView myTextView;
EditText E1;
EditText E2;
EditText E3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Button b1= (Button) findViewById(R.id.button1);
Button b3= (Button) findViewById(R.id.button3);
E1 = (EditText) findViewById(R.id.editText1);
E2 = (EditText) findViewById(R.id.editText2);
E3 = (EditText) findViewById(R.id.editText3);

b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {




DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder;
try {
docBuilder = docFactory.newDocumentBuilder();


// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Class");
doc.appendChild(rootElement);

// staff elements
Element student = doc.createElement("Student");
rootElement.appendChild(student);


// firstname elements
Element firstname = doc.createElement("firstname");
firstname.appendChild(doc.createTextNode(E1.getText().toString()));
student.appendChild(firstname);

Element Email = doc.createElement("Email");
Email.appendChild(doc.createTextNode(E2.getText().toString()));
student.appendChild(Email);

// nickname elements
Element Roll = doc.createElement("Roll_No");
Roll.appendChild(doc.createTextNode(E3.getText().toString()));
student.appendChild(Roll);


// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();



Transformer transformer = transformerFactory.newTransformer();


DOMSource source = new DOMSource(doc);

File FF=new File(Environment.getExternalStorageDirectory()+"//new.xml");
try {
FF.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

StreamResult result = new StreamResult(FF);




transformer.transform(source, result);

}
catch (ParserConfigurationException e) {

e.printStackTrace();
}
catch (TransformerException e) {

e.printStackTrace();}
Toast.makeText(getApplicationContext(),
Environment.getExternalStorageDirectory().toString(), Toast.LENGTH_LONG).show();


}
});

b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close the application
finish(); }});
}
}















Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.










lang-xml







.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...