vendredi 27 novembre 2015

How to store xml values into sqlite db in android?

Am receiving successfully the xml values in resultString.. am storing it in a buffer buf.. I dont know how to store it to the db since am new to this technology.Kindly help me with code. With this i added my db image. result string am receiving.

xml

<Details><FGS>
  <FG>
    <BOMNO>APPCRORBC      </BOMNO>
    <FITEM>APPCRORBC      </FITEM>
    <ITMDESC>APPLES CRAB 22/23#                                          </ITMDESC>
    <USEBINS>Y</USEBINS>
    <USELOTS>N</USELOTS>
    <BQTYPRD>1.000000</BQTYPRD>
    <WEIGHT>25.000</WEIGHT>
    <STKUMID>CASE  </STKUMID>
  </FG>
  <FG>
    <BOMNO>AVOCADOSALAD   </BOMNO>
    <FITEM>AVOSALAD       </FITEM>
    <ITMDESC xml:space="preserve">                                                            </ITMDESC>
    <USEBINS>Y</USEBINS>
    <USELOTS>N</USELOTS>
    <BQTYPRD>1.000000</BQTYPRD>
    <WEIGHT>0.000</WEIGHT>
    <STKUMID>CASE  </STKUMID>
  </FG>
  <FG>
    <BOMNO>POTDI13        </BOMNO>
    <FITEM>POTDI13        </FITEM>
    <ITMDESC>POTATO DICED 1 in (Large Cut) - Contains Sulphites          </ITMDESC>
    <USEBINS>Y</USEBINS>
    <USELOTS>N</USELOTS>
    <BQTYPRD>5.000000</BQTYPRD>
    <WEIGHT>10.000</WEIGHT>
    <STKUMID>BAG   </STKUMID>
  </FG>
  <LOTBIN>
    <fitem>POTDI13        </fitem>
    <item>POTDI13        </item>
    <uselots>N</uselots>
    <usebins>Y</usebins>
    <lotno xml:space="preserve">        </lotno>
    <qbin>15293          </qbin>
  </LOTBIN>
  <LOTBIN>
    <fitem>POTDI13        </fitem>
    <item>POTDI13        </item>
    <uselots>N</uselots>
    <usebins>Y</usebins>
    <lotno xml:space="preserve">        </lotno>
    <qbin>15295          </qbin>
  </LOTBIN>
  <LOTBIN>
    <fitem>POTDI13        </fitem>
    <item>POTDI13        </item>
    <uselots>N</uselots>
    <usebins>Y</usebins>
    <lotno xml:space="preserve">        </lotno>
    <qbin>A123           </qbin>
  </LOTBIN>
  <LOTBIN>
    <fitem>POTDI13        </fitem>
    <item>POTDI13        </item>
    <uselots>N</uselots>
    <usebins>Y</usebins>
    <lotno xml:space="preserve">        </lotno>
    <qbin>A345           </qbin>
  </LOTBIN>
</FGS></Details>

Code

 @Override
            protected String doInBackground(String... params) {
                // TODO Auto-generated method stub

                List<String[]> resultList = new ArrayList<String[]>();
                String[] usernameArray = null;


                try {
                    SoapObject request = new SoapObject(NAMESPACE,
                            METHOD_GET_LOGIN_DETAILS);
                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                            SoapEnvelope.VER11);
                    request.addProperty("loginUsr", user);
                    request.addProperty("loginPasswd", pass);
                    envelope.dotNet = true;// to handle .net services asmx/aspx
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE ht = new HttpTransportSE(URL_PROTOCOL
                            + mSettings.getMmpServerpath() + URL_SERVICE_NAME,120000
                            );
                    ht.debug = true;
                    String soap_action = NAMESPACE + METHOD_GET_LOGIN_DETAILS;
                    ht.call(soap_action, envelope);


                    SoapPrimitive resultString = (SoapPrimitive) envelope
                            .getResponse();
                    File mImpOutputFile = Supporter.getImpOutputFilePathByCompany(
                            "MobManufacturingProcess", "00", "FiGoodsDetails"
                                    + ".xml");
                    if (!mImpOutputFile.exists()) {
                        Supporter.createFile(mImpOutputFile);
                    } else {
                        mImpOutputFile.delete(); // to refresh the file
                        Supporter.createFile(mImpOutputFile);
                    }

                    BufferedWriter buf = new BufferedWriter(new FileWriter(
                            mImpOutputFile, true));

                    buf.append("<" + "Details" + ">");

                    buf.append(resultString.toString());

                    buf.append("</" + "Details" + ">");
                    result = "success";

                    buf.close();

                } catch (SocketTimeoutException e) {
                    result = "time out error";
                    e.printStackTrace();
                } catch (IOException e) {
                    result = "input error";
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    result = "error";
                    e.printStackTrace();
                } catch (Exception e) {
                    Log.e("tag", "error", e);
                    result = "error";
                }
                new DataLoadToDBOperation().execute();
                return result;  

            }
        }
        // Async task to load XML data into the database
        private class DataLoadToDBOperation extends
                AsyncTask<String, String, String> {
            private String user;
            private String pass;
            private ProgressDialog dialog;

            public DataLoadToDBOperation() {
                dialog = new ProgressDialog(LogInActivity.this);
                dialog.setCancelable(false);
            }

            @Override
            protected String doInBackground(String... params) {

        String errMsg = "";

        try {

            String result = "";

            DataLoader fileLoader = new DataLoader(LogInActivity.this, mDBHelper,
                    user);

            List<String> importFileList = mSupporter.loadImportFileList();

            int totImpFile = importFileList.size();

            File salPer_folder_path = Supporter
                    .getImportFolderPath(user);

            List<String> compList = mSupporter
                    .getFolderNames(salPer_folder_path);

            int compSize = compList.size();

            if (compSize != 0) {

                startDBTransaction("db data loading"); // to start db
                                                        // transaction

                for (int c = 0; c < compList.size(); c++) {

                    String serCompName = compList.get(c);

                    for (int i = 0; i < totImpFile; i++) {

                        String fileName = importFileList.get(i);

                        if ((c > 0) && (fileName.equals("FigoodsDetail"))) {// to
                                                                        // load
                                                                        // FigoodsDetail
                                                                        // data
                                                                        // once
                            continue; // to continue for other files
                        }

                        mImpOutputFile = Supporter
                                .getImpOutputFilePathByCompany(spCode,
                                        serCompName, fileName + ".xml");

                        if (mImpOutputFile.exists()) {
                            InputStream inputStream;

                            inputStream = new FileInputStream(
                                    mImpOutputFile);
                            String[] resultArray = fileLoader
                                    .parseDocument(inputStream);

                            result = resultArray[0];
                            errMsg = resultArray[1];

                            if (!result.equals("success")) {
                                mDBHelper.mEndTransaction();
                                break;
                            }

                        } else {
                            result = "File not available";
                            mDBHelper.mEndTransaction();
                            break;
                        }

                    }

                    if (!result.equals("success")) { // to break from
                                                        // executing other
                                                        // companies
                        break;
                    }

                }
                endDBTransaction(); // to end db transaction

            } else {
                result = "File not available";
            }

            return result;[![Database][1]][1]
        } catch (Exception exe) {
            exe.printStackTrace();
            String errorCode = "Err-CLS-2";
            LogFileCreator.appendLog(errorCode + " : " + exe.getMessage()
                    + "\n" + errMsg);
            String result = "error";
            return result;
        }
    }

Aucun commentaire:

Enregistrer un commentaire