4

I am writing a Android app in Java, I need to populate a SQLite database with data from a xml file, what code can I use to do this?
The XML file will be a Microsoft access Database that has been exported to a XML file, the data in the file needs to be saved in a SQLite database.

I am developing in Eclipse with the Android SDK.

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Gillan
  • 149
  • 1
  • 4
  • 11

3 Answers3

3

You can do with this way

  1. You have parse your XML
  2. Than insert into databse
Community
  • 1
  • 1
Riddhish.Chaudhari
  • 833
  • 1
  • 8
  • 24
1

Add your xml file in assets folder and then parse it by using this..! XmlContentHandler is an handler class which implements DefaultHandler.

        InputStream inputStream;
        try 
        {

            inputStream = getAssets().open("persons.xml");
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLReader xmlReader = saxParser.getXMLReader();

            XmlContentHandler handler =  new XmlContentHandler();

            xmlReader.setContentHandler(handler);
            xmlReader.parse(new InputSource(inputStreamReader));

            ArrayList<Person> array = handler.getArray();
            ArrayAdapter<Person> adapter = new ArrayAdapter<Person>(getApplicationContext(),
                    R.layout.list_item, array);
            listView.setAdapter(adapter);

        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
        catch (ParserConfigurationException e) 
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
Prashant Mishra
  • 627
  • 5
  • 18
0

You have to parse your XML file using a "parser". You should check for JSOUP, XPATH, etc... There are many!

Regards.

Manitoba
  • 8,522
  • 11
  • 60
  • 122