0

I got a class that extends AsyncTask, this class is for generating a XML file. Here is my class

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

}

My XML looks like this:

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

Why doesnt it show the whole content of the ArrayList<Case> ?

1
  • Unless you are abnormally fond of the DOM model the XmlSerializer is probably a better choice. Commented Jul 16, 2012 at 14:58

1 Answer 1

1

The issue looks to be that you are appending items before you give them children. I recommend you move this line after the for-loop:

doc.appendChild(rootCase); 

You may also need to refactor the contents of the for-loop with this in mind.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.