0

I don't understand why I'm getting this error, I'm trying to write to a file, not read from it. At first I got an error because I didn't put a jar in the libs folder, now I am getting this error. Here's my code, help would be appreciated! :)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    writeXML();


}




private void writeXML() {

    try{
    Document doc = new Document();

    Element theRoot = new Element("tvshows");
    doc.setRootElement(theRoot);

    Element show = new Element("show");
    Element name = new Element("show");
    name.setAttribute("show_id", "show_001");

    name.addContent(new Text("Life On mars"));

    Element network = new Element("network");
    network.setAttribute("country", "US");

    network.addContent(new Text("ABC"));

    show.addContent(name);
    show.addContent(network);

    theRoot.addContent(show);

    // - -



    Element show2 = new Element("show");
    Element name2 = new Element("show");
    name2.setAttribute("show_id", "show_002");

    name2.addContent(new Text("Life On mars"));

    Element network2 = new Element("network");
    network2.setAttribute("country", "US");

    network2.addContent(new Text("ABC"));

    show2.addContent(name2);
    show2.addContent(network2);

    theRoot.addContent(show2);

    XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
    xmlOutput.output(doc, new FileOutputStream(new File("C:\\Users\\jmckay\\Desktop\\jdomMade.xml")));


    TextView tv = (TextView)findViewById(R.id.text);
    tv.setText("done");
    }catch(Exception e){
        e.printStackTrace();
    }
}
Cj1m
  • 805
  • 3
  • 11
  • 26

1 Answers1

1

It looks like you're trying to save to a Windows file address, which you can't do from an Android device (emulated or otherwise). You can use openFileOutput to get a FileOutputStream to write to a file in internal storage (specific to your app):

xmlOutput.output(doc, openFileOutput(yourFilename, Context.MODE_PRIVATE));

Have a look at this developer guide on Storage Options, in particular Internal and External Storage.

danj1974
  • 481
  • 2
  • 12
  • I have one more question, I now want to read from the file. What directory is my xml in. I need it for this Document readDoc = builder.build(new File(directory)); – Cj1m Aug 04 '13 at 15:22
  • You can just use the same approach with 'openFileInput(yourFilename)' and pass that to the 'build' method. Also if you want a quick way to access the files in the real world then try the workaround in this question to e-mail them to yourself: http://stackoverflow.com/questions/6072895/email-from-internal-storage and the answers by Chris Stratton and Dmitry Kochin. Probably not recommended for a production approach though. – danj1974 Aug 04 '13 at 15:26