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...

Expected a key while parsing a block mapping (Flutter)

Flutter makes use of the Dart packaging system, pub. Via your applications  pubspec.yam l file (or simple pubspec), you can pull down packages from the flutter ecosystem, or the broader dart community. Anyway, i need to add some images to my flutter application, and so had to add an assets section to the pubspec .the default Android Studio generated apps pubspec has a lot of commented out code with explainations about what is going on, e.g # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.io/assets-and-images/#resolution-aware. So I uncommented these lines # assest : - - - #  -  images/a_dot_ham.jpeg with the idea from these comment.  that i would just edit it to suit my particular needs. Once you have edited your  pubspec, you need to click on the "Get dependenc...

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" ); ...