Skip to main content

Create XML file from Arraylist in Android

                   Convert Arraylist data into XML Data 
1. Declare an Arraylist outside the onCreate method
ArrayList 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

Popular posts from this blog

How to align Title at center of ActionBar in Android

How to Align Title At Center of Action Bar in Android                                                                                                                                                @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("DashBoard"); }   Activity  public void setTitle ( String title ){ getSupportActionBar (). setHomeButtonEnabled ( true ); getSupportActionBar (). setDisplayHomeAsUpEnabled ( true ); TextView textView = new TextView ( this ); textView . set...

SQLite Database With Android

Android provides several ways to store user and app data. SQLite is one way of storing user data. SQLite is a very light weight database which comes with Android OS. In this tutorial I’ll be discussing how to write classes to handle all SQLite operations. SQLite is a opensource  SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation. SQLite supports all the relation database features. In order to access this database you don't need to establish any kind of connections for it like JDBCODBC etc. In this tutorial I am taking an example of storing user Detail in SQLite database. I am using a table called UserMaster to store user detail. This table contains three columns  id (INT) , userid (VARCHAR) , username (VARCHAR) , password (VARCHAR) , address (VARCHAR) . Database- Package The main package is android.database.sqlite that contains the classes to mange your own database. In this Class we are creat...

Difference Between Pending Intent And Intent in Android

                       Normal Intent       Normal Intent will die as soon as the app being killed.    An Android Intent is an object carrying an intent, i.e a message from one Component to another     Component either inside or outside of the application.Intent can communicate message among     any of the three core Components of an application -- Activities, Services,and BroadcastReceivers.     Two types of Intent in Android   1. Explicit Intent.   2.Implicit Intent  Explicit Intent is an Intent which is used to Call the another component Explicitly in your application  Like :We are calling  Next activity on button click of First activity using Intent Example  // Explicit Intent by specifying its class name Intent i = new Intent ( this , TargetActivity . class ); i . putExtra ( "Key1" , "ABC" ); ...