-3

I have several xml files on sdcard. How parse each of this xml files and insert received data to database?

I receive names of files in folder but don't know what and how do next?

File sdPath = Environment.getExternalStorageDirectory();
File sdPathDr = new File(sdPath.getAbsolutePath() + "/" + "Android/data/" + getPackageName() + "/" + DIR_SD);

File[] files = sdPathDr.listFiles();

If can please write sample of code.

Sample of xml file

 <row name="Rutracker">
 <col name="title_enter"> Rutracker </col>
 <col name="login_enter"> bmF6MsdghDEzvcgdghd </col>
 <col name="password_enter"> MXFhsejJ3c3g=sdfsdggd </col>
 <col name="link_enter"> http://www.rutracker.org </col>
 <col name="comment_enter"> </col>
 <col name="date_enter"> 15.02.2014 </col>
 </row>

2 Answers2

5

Its pretty simple. I will not be giving you any code, you will have to find it out yourself. I am pointing out just the steps:

  1. Get the absolute path of the file
  2. Read the File from the sdcard and store the data using some Collection
  3. Connect to the database using some JDBC (depending on the choice of your database)
  4. Persist the collection into the database.

Now, each of these steps have pretty simple codes. You can search them and make up your program. ALL THE BEST !

Edit for adding additional data as asked by the user

You can create a bean class of Rutracker with your column names as the variables. Then you can create a List<Rutracker> while reading the different files and atlast persist the list in the database.

Please go through the following link for a better idea !

https://stackoverflow.com/a/21329056/1759128

Community
  • 1
  • 1
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
0

Your question is somewhat general, but I would recommend that you search for "java parsing XML". You will find many results to carry you to your solution, like http://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html

If your xml is very simple, you might even write a small parser of your own.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37