我有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();
    }

}

09-03 21:46