-1

I need a little bit of help...I want to write scores to an XML file on click of a save button and retrieve saved scores automatically the next time the app starts up.

I have written the following code to save the score but unfortunately it's not working. No file is created at all:

    public void savescore() throws TransformerException, ParserConfigurationException {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xmlDoc = docBuilder.newDocument();
    Element scores = xmlDoc.createElement("score");

    Element adityag= xmlDoc.createElement("aditya");
    adityag.appendChild(xmlDoc.createTextNode(adityas.getText().toString()));
    Element ameyg= xmlDoc.createElement("amey");
    ameyg.appendChild(xmlDoc.createTextNode(ameys.getText().toString()));
    Element akshadag= xmlDoc.createElement("akshada");
    akshadag.appendChild(xmlDoc.createTextNode(akshadas.getText().toString()));
    Element anirudhg= xmlDoc.createElement("anirudh");
    anirudhg.appendChild(xmlDoc.createTextNode(anirudhs.getText().toString()));
    Element aashig= xmlDoc.createElement("aashi");
    aashig.appendChild(xmlDoc.createTextNode(aashis.getText().toString()));
    Element dhvanig= xmlDoc.createElement("dhvani");
    dhvanig.appendChild(xmlDoc.createTextNode(dhvanis.getText().toString()));
    Element mering= xmlDoc.createElement("merin");
    mering.appendChild(xmlDoc.createTextNode(merins.getText().toString()));
    Element swapnilg= xmlDoc.createElement("swapnil");
    swapnilg.appendChild(xmlDoc.createTextNode(swapnils.getText().toString()));
    Element architg= xmlDoc.createElement("archit");
    architg.appendChild(xmlDoc.createTextNode(archits.getText().toString()));
    Element shreyag= xmlDoc.createElement("shreya");
    shreyag.appendChild(xmlDoc.createTextNode(shreyas.getText().toString()));

    scores.appendChild(adityag);
    scores.appendChild(ameyg);
    scores.appendChild(akshadag);
    scores.appendChild(anirudhg);
    scores.appendChild(aashig);
    scores.appendChild(dhvanig);
    scores.appendChild(mering);
    scores.appendChild(shreyag);
    scores.appendChild(swapnilg);
    scores.appendChild(architg);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "userData.xml"));
    transformer.transform(source, result);
}

What is going wrong with my code?

EDIT: Empty XML file is now created after adding the permission suggested by @Gabriella

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
volt
  • 21
  • 4

1 Answers1

0

Add the permission for writing to the external storage into your manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

or take a look at this question: how to create xml file in android

EDIT:

I think that your code should looks like this, because you are appending now empty elements to the xml document:

public void savescore() throws TransformerException, ParserConfigurationException {

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document xmlDoc = docBuilder.newDocument();
    Element scores = xmlDoc.createElement("score");

    Element adityag= xmlDoc.createElement("name1");
    adityag.appendChild(xmlDoc.createTextNode(name1.getText().toString()));
    Element ameyg= xmlDoc.createElement("name2");
    ameyg.appendChild(xmlDoc.createTextNode(name2.getText().toString()));
    Element akshadag= xmlDoc.createElement("name3");
    akshadag.appendChild(xmlDoc.createTextNode(name3.getText().toString()));
    Element anirudhg= xmlDoc.createElement("name4");
    anirudhg.appendChild(xmlDoc.createTextNode(name4.getText().toString()));
    Element aashig= xmlDoc.createElement("name5");
    aashig.appendChild(xmlDoc.createTextNode(aname5.getText().toString()));
    Element dhvanig= xmlDoc.createElement("name6");
    dhvanig.appendChild(xmlDoc.createTextNode(name6.getText().toString()));
    Element mering= xmlDoc.createElement("name7");
    mering.appendChild(xmlDoc.createTextNode(name7.getText().toString()));
    Element swapnilg= xmlDoc.createElement("name8");
    swapnilg.appendChild(xmlDoc.createTextNode(name8.getText().toString()));
    Element architg= xmlDoc.createElement("name9");
    architg.appendChild(xmlDoc.createTextNode(name9.getText().toString()));
    Element shreyag= xmlDoc.createElement("name10");
    shreyag.appendChild(xmlDoc.createTextNode(name10.getText().toString()));

    scores.appendChild(adityag);
    scores.appendChild(ameyg);
    scores.appendChild(akshadag);
    scores.appendChild(anirudhg);
    scores.appendChild(aashig);
    scores.appendChild(dhvanig);
    scores.appendChild(mering);
    scores.appendChild(swapnilg);
    scores.appendChild(architg);
    scores.appendChild(shreyag);

    xmlDoc.appendChild(scores);

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(xmlDoc);
    StreamResult result = new StreamResult(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "RummyScore" + "/" + "userData.xml"));
    transformer.transform(source, result);
}

are name1, nam2, name3 etc TextViews?

Community
  • 1
  • 1
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • I have added that permission.. now empty xml file is created. – volt Jul 10 '15 at 08:44
  • where do you define `name1`, `name2` etc? Should they not for example like this: `Element adityag= xmlDoc.createElement("name1");` `adityag.appendChild(xmlDoc.createTextNode(adityag.getText().toString()));` ? – Gabriella Angelova Jul 10 '15 at 09:00
  • you never append the `Element`s like `adityag` to the root. Here you could find a sample example for the same xml-document-creation http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/ – Gabriella Angelova Jul 10 '15 at 09:01
  • I was trying to keep those names secret but forgot to edit there (post edited now).. and i have followed that tutorial only.. – volt Jul 10 '15 at 09:13
  • please try with the edit of my answer, I think that it could help you, you should append to the root element the `Element` nodes not the TextViews – Gabriella Angelova Jul 10 '15 at 09:15
  • and don't forget the line `xmlDoc.appendChild(scores);` ,because now you don't add anything to your xml file – Gabriella Angelova Jul 10 '15 at 09:17
  • Yes! My code is exactly same as your code! Still it is creating empty XML file! Edit: Not hiding names now! See main post Edited! :( – volt Jul 10 '15 at 09:17
  • Check if your name1, name2 etc have something in them and don't forget the `xmlDoc.appendChild(scores);` line ! – Gabriella Angelova Jul 10 '15 at 09:22