/**
* Use the API in Android to parse the XML file in Assets. This is required. It doesn't work if it comes with Java, as if it was compiled .....
*
* @ Param fileName: the name of the xml file to be opened must be in the assets folder. Example: "firstData/bg1_1_10.xml"
* @ Param tbName: name of the table to be inserted into the database. The table structure in the database must correspond one to one with the data structure in the XML file. Otherwise, an error occurs when the database is inserted.
*/
1/*** use the Android API to parse the XML file in Assets. It seems that this is required. It doesn't work if it comes with Java, as if it was compiled ..... 3*4 * @ param fileName: the name of the xml file to be opened must be in the assets folder. For example, "firstData/bg1_1_10.xml" 5 * @ param tbName: the name of the table to be inserted into the database. The table structure in the database must one-to-one match with the data structure in the XML file, otherwise, Error 6 */7 private void insertDataFromAssetsXML (String fileName, String tbName) {8 try {9 XmlPullParserFactory factory; 10 factory = XmlPullParserFactory. newInstance (); 11 factory. setNamespaceAware (true); 12 XmlPullParser xpp; 13 xpp = factory. newPullParser (); 14 xpp. setInput (getResources (). getAssets (). open (FileName), null); 15 16 int eventType = xpp. getEventType (); 17 boolean needs = false; 18 String SQL = ""; // insert Database SQL statement 19 SQL = "insert into" + tbName + "values ("; 20 21 db. sld. beginTransaction (); // start transaction processing 22 while (eventType! = XmlPullParser. END_DOCUMENT) {23 24 if (eventType = XmlPullParser. START_DOCUMENT) {25} else if (eventType = XmlPullParser. START_TAG) {26 if (! Xpp. getName (). equals ("RECORD ")&&! Xpp. getName (). equals ("RECORDS") {27 needs = true; 28} 29} else if (eventType = XmlPullParser. END_TAG) {30 // Log. e ("xml", "End tag" + xpp. getName (); 31 if (xpp. getName (). equals ("RECORD") {32 SQL = SQL. substring (0, SQL. length ()-1); 33 SQL + = ");"; 34 SQL = SQL. replace ("\ n", ""); 35 // Log. w ("SQL", SQL); 36 db. insert (SQL); // insert database 37 SQL = "insert into" + tbName + "values ("; 38} 39} else if (EventType = XmlPullParser. TEXT) {40 if (! Xpp. getText (). equals ("") & needs) {41 SQL + = "'" + xpp. getText () + "',"; 42 needs = false; 43} 44} 45 eventType = xpp. next (); 46} 47 48 db. sld. setTransactionSuccessful (); // start transaction processing 49 db. sld. endTransaction (); // transaction processing completed 50 51} catch (XmlPullParserException e) {52 e. printStackTrace (); 53} catch (IOException e) {54 e. printStackTrace (); 55} catch (Exception e) {56 e. printStackTrace (); 57} 58}
Use: insertDataFromAssetsXML ("firstData/imp_multilevel.xml", "imp_multilevel ");
Imp_multilevel is the name of the sqlite database table.
In addition, the above db is the amount of sqlite database objects. It's okay to add it on your own !!!