How to populate a SQLite database with data from an XML file?

I am writing an Android application in Java, I need to populate the SQLite database with data from an XML file, what code can I use for this?
The XML file will be a Microsoft database that was exported to an XML file, the data in the file must be stored in the SQLite database.

I am developing in Eclipse with the Android SDK.

+4
source share
3 answers

Add your xml file to the resources folder and then parse it using this ..! XmlContentHandler is a handler class that 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(); } 
+1
source

You need to parse the XML file using a parser. You should check out JSOUP, XPATH, etc. A lot of them!

Sincerely.

0
source

Source: https://habr.com/ru/post/1403819/


All Articles