android - 将Db值导出为XML文件

标签 android sqlite export

我有5个EditText值,其中值将来自db。我需要将这5个值导出为XML文件。我不知道该怎么做,因为这是Android技术的新手。请帮助我的代码。

 submit_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final AutoCompleteTextView lot_binautoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.lot_binAutoComplete);
                final AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.searchAutoComplete);

                String fiGoods = autoCompleteTextView.getText().toString()
                        .trim();

                String fiQty = editFiQty.getText().toString().trim();
                String fiUnWeight = editFiUnWeight.getText().toString().trim();
                String fiTotWeight = editFiTotWeight.getText().toString()
                        .trim();
                String lotBin = lot_binautoCompleteTextView.getText()
                        .toString().trim();

                if(fiQty.isEmpty()||fiUnWeight.isEmpty()||fiTotWeight.isEmpty()||lotBin.isEmpty()){
                    nullConfirmation();
                }else{

                mDBHelper.getWritableDatabase();
                mDBHelper.updatefiGoodsDetail(fiGoods, fiQty, fiUnWeight,
                        fiTotWeight, lotBin);
                mDBHelper.closeDatabase();


                Intent UpdAct = new Intent(MainMenu.this, ItemSummary.class);
                UpdAct.putExtra("finishGoods", fiGoods);
                UpdAct.putExtra("finishQty", fiQty);
                UpdAct.putExtra("finishUnWeight", fiUnWeight);

                startActivity(UpdAct);

            //  mSupporter.navigateTo(ItemSummary.class);
            }}
        });

最佳答案

你可以试试这个:

private void addNewObject(String fiQty, String fiUnWeight, String fiTotWeight, String lotBin) {

    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("root");
        doc.appendChild(rootElement);

        // elements
        Element obj = doc.createElement("object");
        rootElement.appendChild(obj);

        Element fiQtyE = doc.createElement("fiQty");
        fiQtyE.appendChild(doc.createTextNode(fiQty));
        obj.appendChild(fiQtyE);
        //  elements
        Element fiUnWeightE = doc.createElement("fiUnWeight");
        fiUnWeightE.appendChild(doc.createTextNode(fiUnWeight));
        obj.appendChild(fiUnWeightE);

        Element fiTotWeightE = doc.createElement("fiTotWeight");
        fiTotWeightE.appendChild(doc.createTextNode(fiTotWeight));
        obj.appendChild(fiTotWeightE);

        //  elements
        Element lotBinE = doc.createElement("lotBin");
        lotBinE.appendChild(doc.createTextNode(lotBin));
        obj.appendChild(lotBinE);

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(new File(Config.FILE_NAME));//your dir file

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);

        System.out.println("File saved!");

    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    }

}

关于android - 将Db值导出为XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33887990/

相关文章:

java - Android 淡入图像

java - 当用户将对象拖动到边界附近时滚动列表

r - 加载 R 包时连接到数据库

java - 创建一个快速的 Android 词典(字数统计)

export - 如何仅将photoshop中的选定图层导出为单独的png图像

php - 以 csv 格式导入和导出所有类别

oracle - 验证 Oracle dmp

android - 在 Android 中实现 MVC 的好处

android - 以编程方式启用自定义键盘

java - SQLite - 选择不同日期的第一条记录