What is a XSream ?
XSream :- XStream is a Simple Java Based Library to Serialize Java Object to XML. XStream is a light weight, fastest and provide Default Maping for most the object to serialize.
XStream serializes used some internal fields e.g- private ,final, non-public and inner class. XStream Provide Security issues etc.
Example- Show xml formate.
<com.project.example.makexmlapp.Person>
<Name>Android Beginner point</Name>
<Number>120-4122333</Number>
</com.project.example.makexmlapp.Person>
Explain:- How to implement XStream Xml step by step?
First you can add .jar file inside lib folder our android project.
xstream-1.4.4.jar
Create Student class.
Student.Java
............................................................................................................
public class Student { public String Name; public String RollNumber; public String Section; public String ContactNumber; public Student(String Name,String RollNumber,
String Section,String ContactNumber){ this.Name=Name; this.RollNumber=RollNumber; this.Section=Section; this.ContactNumber=ContactNumber; } public Student(){ } public String getRollNumber() { return RollNumber; } public void setRollNumber(String rollNumber) { RollNumber = rollNumber; } public String getSection() { return Section; } public void setSection(String section) { Section = section; } public String getContactNumber() { return ContactNumber; } public void setContactNumber(String contactNumber) { ContactNumber = contactNumber; } public String getName() { return Name; } public void setName(String name) { Name = name; } }............................................................................................................
activity_main.xml
............................................................................................................
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15dp" android:textStyle="bold" android:id="@+id/text"/> </RelativeLayout>............................................................................................................
MainActivity.java
............................................................................................................
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView vieatxt=(TextView)findViewById(R.id.text); try { Student record=new Student("Rohit Kumar",
"4122333","B","+91 7953568756"); XStream xstream = new XStream(); String xml = xstream.toXML(record); Log.i("Xml", xml); vieatxt.setText("Output:"+"\n\n"+xml); }catch (Exception e){ e.printStackTrace(); } } }............................................................................................................
Output:-
<com.project.example.makexmlapp.Student>
<ContactNumber>+91 7953568756</ContactNumber>
<Name>Rohit Kumar</Name>
<RollNumber>4122333</RollNumber>
<Section>B</Section>
</com.project.example.makexmlapp.Student>
0 comments