简体   繁体   中英

Inserting a large amount of XML data in my SQLite database

I am making an app in Android which communicates with a .NET Webservice to get Data.
I have got data in XML format and also parsed it, but I am not able to insert it into my SQLite database.
The data amount is very large and I am confused on how to insert so many data through looping.
The XML data is in the following format.

     <NewData>
              <Table>
              <First Name>Sangeeta</ First  Name>
              <Second Name>Rawat</Second Name>
              <Designation>Tester</Designation>
               .
               .
               .

              </Table>
    </New Data>

my code is

       public static  void invokeHelloWorldWS(String webMethName,Context context) {
    String resTxt = null;
    mDbHelper = new TestAdapter(context);
    // Create request
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, webMethName);

    // Property which holds input parameters
    //PropertyInfo celsiusPI = new PropertyInfo();
    // Set Name
    ////celsiusPI.setName("name");
    // Set Value
    //celsiusPI.setValue(name);
    // Set dataType
    //celsiusPI.setType(String.class);
    // Add the property to request object
    //request.addProperty(celsiusPI);
    //request.addProperty("Content-Type", "text/xml; charset=utf-8");
    // Create envelope
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
            SoapEnvelope.VER11);

    envelope.dotNet = true;
    // Set output SOAP object
    envelope.setOutputSoapObject(request);
    // Create HTTP call object
    HttpTransportSE  androidHttpTransport = new HttpTransportSE (SOAP_ADDRESS);
    androidHttpTransport.debug = true;

    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
    String xml = androidHttpTransport.responseDump;
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
         factory.setNamespaceAware(true);
         XmlPullParser xpp = factory.newPullParser();

         xpp.setInput( new StringReader ( xml ) );
         Log.e("xml", "loaded");
         int eventType = xpp.getEventType();
         xpp.require(XmlPullParser.START_DOCUMENT, null, null);

         while (xpp.next() != XmlPullParser.END_TAG) {
             if (eventType == XmlPullParser.START_TAG) {
                Log.e(eventType+"", XmlPullParser.START_TAG+"");
                 continue;

             }
             String name = xpp.getName();
             Log.e("out", name);
             if (name.equals("CONS_REF")) {
                CONS_REF = readTitle(xpp);
                Log.e("CONS_REF", "i m here");
                Log.e("CONS_REF", CONS_REF);
             } else if (name.equals("BILL_MTH")) {
                BILL_MTH = readSummary(xpp);
                Log.e("BILL_MTH", "im here");
                Log.e("BILL_MTH", BILL_MTH);
             } else if (name.equals("NAME")) {

             } else {
                 Log.e("skip", "i m in skip");
                 skip(xpp);
             }


         }


    } catch (Exception e) {
        e.printStackTrace();
        //resTxt = "Error occured";
    } 


}


private static  String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, null, "CONS_REF");
    String title = readText(parser);
    parser.require(XmlPullParser.END_TAG, null, "CONS_REF");
    return title;
    }

       private static String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;

      }

      private static void skip(XmlPullParser parser) throws XmlPullParserException,   IOException {
    if (parser.getEventType() != XmlPullParser.START_TAG) {
       // throw new IllegalStateException();
        Log.e(parser.getEventType()+"",XmlPullParser.START_TAG+"          inside    skip" );
    }
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
        case XmlPullParser.END_TAG:
                depth--;
                break;
        case XmlPullParser.START_TAG:
                depth++;
                break;
         }
       }
    }


    private static String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, null, "BILL_MTH");
    String summary = readText(parser);
    parser.require(XmlPullParser.END_TAG, null, "BILL_MTH");
    return summary;
}

}

you can do it in 2 ways

  1. with data base

    1. compress data interms of binary.
    2. store in database as blob.
    3. retrive as blob , decompress (pain full) and use it
  2. with files(i suugest better approch)

    1. store as text file, keep its reference in database.

    2. while accessing read its reference and then read the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM