Convert Arraylist data into XML Data
1. Declare an Arraylist outside the onCreate methodArrayList savejsonarray;
2. Create an object of Arraylist
String tempXml = makeSendingData(savejsonarray);
Example:
package com.lpu.lpuutilities;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
public class MeterReadingBH7_BH8 extends ActionBarActivity {
ArrayList mLevelArray, savejsonarray;
Button syncdata;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_meter_reading_bh7__bh8);syncdata = (Button) findViewById(R.id.syncbtn);syncdata.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { tempXml = makeSendingData(savejsonarray);Log.d("tempxml", tempXml);}});
}
/* For creating XML FILE*/
public String makeSendingData(ArrayList myarr) throws Exception { String xmlString = ""; Log.d("inmakesendingdata", "asd"); xmlString = writeUsingXMLSerializer(myarr); return xmlString; } public String writeUsingXMLSerializer(ArrayList<sourcegettersetter> list) throws Exception { Log.d("mylist123", String.valueOf(list.size())); XmlSerializer xmlSerializer = Xml.newSerializer(); StringWriter writer = new StringWriter();xmlSerializer.setOutput(writer); // start DOCUMENT xmlSerializer.startDocument("UTF-16", true); // open tag: <record> for (int i = 0; i < list.size(); i++) { sourcegettersetter obj = list.get(i); xmlSerializer.startTag("", "Meter"); // open tag: <study> xmlSerializer.startTag("", "HostelId"); xmlSerializer.text(obj.getHostel_ID()); xmlSerializer.endTag("", "HostelId"); // open tag: <study> xmlSerializer.startTag("", "RoomTypeId"); xmlSerializer.text(obj.getReading_Type()); xmlSerializer.endTag("", "RoomTypeId"); // open tag: <study> xmlSerializer.startTag("", "RoomNo"); xmlSerializer.text(obj.getRoom_No()); xmlSerializer.endTag("", "RoomNo"); // open tag: <study> xmlSerializer.startTag("", "LightReading"); xmlSerializer.text(obj.getLight_Reading()); xmlSerializer.endTag("", "LightReading"); // open tag: <study> xmlSerializer.startTag("", "LightRemarks"); xmlSerializer.text(obj.getLight_remarks()); xmlSerializer.endTag("", "LightRemarks"); // open tag: <study> xmlSerializer.startTag("", "HostelLevel"); xmlSerializer.text(obj.getHostel_Level()); xmlSerializer.endTag("", "HostelLevel"); // open tag: <study> xmlSerializer.startTag("", "ReadingNumber"); xmlSerializer.text(obj.getReading_No()); xmlSerializer.endTag("", "ReadingNumber"); // open tag: <study> xmlSerializer.startTag("", "PowerReading1"); xmlSerializer.text(obj.getPower_Meter1()); xmlSerializer.endTag("", "PowerReading1"); // open tag: <study> xmlSerializer.startTag("", "PowerRemarks1"); xmlSerializer.text(obj.getPower_Remarks1()); xmlSerializer.endTag("", "PowerRemarks1"); // open tag: <study> xmlSerializer.startTag("", "PowerReading2"); xmlSerializer.text(obj.getPower_Meter2()); xmlSerializer.endTag("", "PowerReading2"); // open tag: <study> xmlSerializer.startTag("", "PowerRemarks2"); xmlSerializer.text(obj.getPower_Remarks2()); xmlSerializer.endTag("", "PowerRemarks2"); // open tag: <study> xmlSerializer.startTag("", "PowerReading3"); xmlSerializer.text(obj.getPower_Meter3()); xmlSerializer.endTag("", "PowerReading3"); // open tag: <study> xmlSerializer.startTag("", "PowerRemarks3"); xmlSerializer.text(obj.getPower_Remarks3()); xmlSerializer.endTag("", "PowerRemarks3");xmlSerializer.endTag("", "Meter"); } // end DOCUMENT xmlSerializer.endDocument();return writer.toString().replace("'", "\""); }
}
This code is work for me.If you have any query Please write below inside the comment section
Comments
Post a Comment