Reputation: 159
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
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
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