0

我有一个扩展类AsyncTask,这个类用于生成 XML 文件。这是我的课

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;

import android.util.Log;
import android.widget.Toast;

public class GenerateXML extends AsyncTask<String, Void, String> {

private final static String TAG = "GENECREATE_XML: "; 

DocumentBuilder docBuilder; 
DocumentBuilderFactory docFactory;

ArrayList<Case> caseArray;
Context c;

ProgressDialog pd; 

String address; 



public GenerateXML(ArrayList<Case> caseArray, Context c) {

    caseArray = new ArrayList<Case>(); 

    this.caseArray = caseArray; 
    this.c = c; 

    address = "/sdcard/case.xml";

}

protected void onPreExecute() {

      pd=ProgressDialog.show(c,"Please Wait..","Generating XML",false);

}

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

    int i = 0; 

    docFactory = DocumentBuilderFactory.newInstance();

    try {
         docBuilder = docFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        Log.e(TAG, e.getMessage()); 
    }

    Document doc = docBuilder.newDocument(); 

    //root element in the xml
    Element rootCase = doc.createElement("Cases"); 
    doc.appendChild(rootCase);


    for(Case cases : caseArray) {

    // case elements
    Element caseElement = doc.createElement("case");
    rootCase.appendChild(caseElement);

    // set attribute to staff element
    Attr attr = doc.createAttribute("id");
    attr.setValue(Integer.toString((++i)));
    caseElement.setAttributeNode(attr);

    // pcn
    Element pcn = doc.createElement("pcn");
    pcn.appendChild(doc.createTextNode(cases.getCaseNumber()));
    caseElement.appendChild(pcn);

    //status
    Element status = doc.createElement("status");
    status.appendChild(doc.createTextNode(Integer.toString(cases.getStatus())));
    caseElement.appendChild(status);

    //date
    Element date = doc.createElement("date");
    date.appendChild(doc.createTextNode(cases.getDate()));
    caseElement.appendChild(pcn);

    }


    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = transformerFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        Log.e(TAG, e.getMessage()); 
    }

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(address));



    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        Log.e(TAG, e.getMessage());
    }


    return null;
}

protected void onPostExecute(String result) {
    pd.dismiss(); 
    Toast.makeText(c, "Finished. File saved to: " + address, Toast.LENGTH_LONG).show(); 
}

}

我的 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Cases/>

为什么不显示整个内容ArrayList<Case>

4

1 回答 1

1

问题似乎是您在给他们孩子之前附加项目。我建议你在 for 循环之后移动这一行:

doc.appendChild(rootCase); 

考虑到这一点,您可能还需要重构 for 循环的内容。

于 2012-07-16T13:43:29.650 回答