拡張するクラスを取得しました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>
ですか?