XML无法正确生成android

时间:2012-07-16 13:32:45

标签: android

我有一个扩展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>的全部内容?

1 个答案:

答案 0 :(得分:1)

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

doc.appendChild(rootCase); 

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