Gillan
Gillan

Reputation: 159

How to populate a SQLite database with data from a xml file?

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.

Upvotes: 4

Views: 2666

Answers (3)

Prashant Mishra
Prashant Mishra

Reputation: 627

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();
        }

Upvotes: 1

Riddhish.Chaudhari
Riddhish.Chaudhari

Reputation: 853

You can do with this way

  1. You have parse your XML
  2. Than insert into databse

Upvotes: 3

Manitoba
Manitoba

Reputation: 8702

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

Regards.

Upvotes: 0

Related Questions