在我的SQLite数据库中插入大量XML数据

时间:2014-01-04 11:38:43

标签: android web-services sqlite xml-parsing

我正在Android中创建一个应用程序,它与.NET Web服务进行通信以获取数据 我有XML格式的数据并解析它,但我无法将其插入到我的SQLite数据库中 数据量非常大,我对如何通过循环插入如此多的数据感到困惑 XML数据采用以下格式。

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

              </Table>
    </New Data>

我的代码是

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

}

1 个答案:

答案 0 :(得分:0)

你可以用两种方式做到这一点

  1. 包含数据库

    1. 压缩二进制数据。
    2. 以blob形式存储在数据库中。
    3. 作为blob回复,解压缩(充满痛苦)并使用它
  2. 带文件(我认为更好的approch)

    1. 存储为文本文件,将其引用保存在数据库中。

    2. 访问时读取其引用,然后读取该文件。