0

Hello I have an application that I need to convert Sqlite table to Xml and save it to the external storage to be able to use later.

I have succesfully created the Sqlite table on runtime. It has 13 columns. Each row represents an object(product) and each column should be a tag in Xml file.

Here is the part I tried to create the Xml file

while(cursor2.moveToNext()) {
        veriler.add(cursor2.getInt(0) + "-" + cursor2.getString(1) + "-" + cursor2.getString(2) + "-" + cursor2.getString(3) + "-" + cursor2.getString(4) + "-" + cursor2.getString(5) + "-" + cursor2.getString(6) + "-" + cursor2.getString(7) + "-" + cursor2.getString(8) + "-" + cursor2.getString(9) + "-" + cursor2.getString(10) + "-" + cursor2.getString(11) + "-" + cursor2.getInt(12) + "-" + cursor2.getInt(13));
        logolustur.WriteLogInformation(cursor2.getString(1),cursor2.getString(2),cursor2.getString(3),cursor2.getString(4),cursor2.getString(5),cursor2.getString(6),cursor2.getString(7),cursor2.getString(8),cursor2.getString(9),cursor2.getString(10),cursor2.getString(11),cursor2.getInt(12),cursor2.getInt(13));
    }

First column of the table is id column which I dont need as a tag in Xml that is why I didnt send it to my WriteLogInformation method.

Here is my WriteLogInformation method(to create the xml) looks like:

public void WriteLogInformation(String aracno, String siparisno, String urunadstr, String urunkodstr, String serinostr, String topukstr, String strKullaniciAd, String islemtipi, String okutmatipi, String mno, String hata, int siparismiktari, int teslimmiktari) {
    String path=context.getFilesDir().getAbsolutePath()+"/log2.xml";
    File file = new File( path );
        try {
            fos= context.getApplicationContext().openFileOutput("log2.xml",context.MODE_PRIVATE);

            serializer.setOutput(fos, "UTF-8");
            serializer.startDocument(null, Boolean.valueOf(true));
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);

            serializer.startTag(null, "XML");
            serializer.startTag(null, "XMLDATA");

            serializer.startTag(null, "ARACNO");
            serializer.text(aracno);
            serializer.endTag(null, "ARACNO");

            serializer.startTag(null, "PN_SIPARIS_NO");
            serializer.text(siparisno);
            serializer.endTag(null, "PN_SIPARIS_NO");

            serializer.startTag(null, "URUNADI");
            serializer.text(urunadstr);
            serializer.endTag(null, "URUNADI");

            serializer.startTag(null, "URUNKODU");
            serializer.text(urunkodstr);
            serializer.endTag(null, "URUNKODU");

            serializer.startTag(null, "BARKOD");
            serializer.text(serinostr);
            serializer.endTag(null, "BARKOD");

            serializer.startTag(null, "TOPUKKODU");
            serializer.text(topukstr);
            serializer.endTag(null, "TOPUKKODU");

            serializer.startTag(null, "SOFORADI");
            serializer.text(strKullaniciAd);
            serializer.endTag(null, "SOFORADI");

            serializer.startTag(null, "ISLEMTIPI");
            serializer.text(islemtipi);
            serializer.endTag(null, "ISLEMTIPI");

            serializer.startTag(null, "OKUTMATIPI");
            serializer.text(okutmatipi);
            serializer.endTag(null, "OKUTMATIPI");

            serializer.startTag(null, "MUSTERIADI");
            serializer.text(mno);
            serializer.endTag(null, "MUSTERIADI");

            serializer.startTag(null, "HATA");
            serializer.text(hata);
            serializer.endTag(null, "HATA");

            serializer.startTag(null, "MIKTAR");
            serializer.text(String.valueOf(siparismiktari));
            serializer.endTag(null, "MIKTAR");

            serializer.startTag(null, "TESLIMMIKTAR");
            serializer.text(String.valueOf(teslimmiktari));
            serializer.endTag(null, "TESLIMMIKTAR");
            serializer.endTag(null,"XMLDATA");

            serializer.endDocument();
            serializer.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

It gives no error but I can not check if I created Xml file an if it was saved to External storage.

My desired Xml should look like :

<XMLDATA>
    <ARACNO>AKO_448846</ARACNO>
    <ARACADI>KIRŞEHİR DENEME 28.4.</ARACADI>
    <SOFORADI>AHMET TALHA</SOFORADI>
    <PN_SIPARIS_NO>C003771814</PN_SIPARIS_NO>
    <MUSTERIADI>MAHMUT ILHAN - OTO LASTIK TAMIR ????</MUSTERIADI>
    <ADRES>ORTA MAH.SANAYI SITESI NO:35 BOZTEPE KIRŞEHİR</ADRES>
    <URUNKODU>5799</URUNKODU>
    <URUNADI>12.5/80-15.3 16PR IMF-18 TL 144 A8</URUNADI>
    <BARKOD>5799/00003597</BARKOD>
    <TOPUKKODU>30776212</TOPUKKODU>
    <MIKTAR>1</MIKTAR>
    <TESLIMMIKTAR>0</TESLIMMIKTAR>
</XMLDATA>
<XMLDATA>
    <ARACNO>AKO_448846</ARACNO>
    <ARACADI>KIRŞEHİR DENEME 28.4.</ARACADI>
    <SOFORADI>AHMET TALHA</SOFORADI>
    <PN_SIPARIS_NO>C003771810</PN_SIPARIS_NO>
    <MUSTERIADI>MAHMUT ILHAN - OTO LASTIK TAMIR ????</MUSTERIADI>
    <ADRES>ORTA MAH.SANAYI SITESI NO:35 BOZTEPE KIRŞEHİR</ADRES>
    <URUNKODU>8470</URUNKODU>
    <URUNADI>900-16  (V3-02-8)</URUNADI>
    <BARKOD />
    <TOPUKKODU />
    <MIKTAR>4</MIKTAR>
    <TESLIMMIKTAR>0</TESLIMMIKTAR>
</XMLDATA>

So is there a better/simplier option to do it?Or Is there still hope in my code to have an Xml file in external storage looks like above.

Basically, How to export an sqlite table in xml format to sd card?. As each column of each row in the table will be a tag and each row will represent a Xml block.

Cem U
  • 893
  • 7
  • 14
  • Out of curiosity, do you have the correct permissions in your manifest? If you do not, you will not be able to write to the SD Card. – miversen33 May 22 '17 at 15:53
  • well yes, ofc I have it. – Cem U May 22 '17 at 16:13
  • `Context#getFilesDir()` returns a directory in your app's internal storage, not the external storage: https://stackoverflow.com/a/17546843. Also note that `WRITE_EXTERNAL_STORAGE` is a dangerous permission: http://stackoverflow.com/questions/32635704. – Mike M. May 22 '17 at 17:39

0 Answers0