-->

XStream Technology java serialization example || serialization in java

ما هي Serialize:
عملية تحويل objects  الي  تيار من البيانات المقسمة ( stream of bytes ) ثم يتم نقلها الي ذاكرة ( memory ) او يتم تخزين الكائن الي ملفات مثل Xml او databases او يمكن نقلها عبر الشبكات او السيرفرات.
الهدف من هذه العملية هو ان تحافظ علي حالة object  في حالة اعطاء امر باسترجاع البيانات او اعادة انشائه من جديد في عملية تسمي deserialization .





ما هي XStream:
هي مكتبة تقوم بعمل تسلسل للكائن (serialize object) وتحويله الي xml  و ارجاعه الي حالته الطبيعية عند استخراج البيانات مرة اخري.







مثال تطبيقي:
صنع Blog Class

package com.Xstream.NST;
public class Blog {
      private String title;
      private String mainTitle;
      private String body;
      private String summary;
      public Blog() {}
      public Blog(String title, String mainTitle, String body, String summary) {
            this.title = title;
            this.mainTitle = mainTitle;
            this.body = body;
            this.summary = summary;
      }
      public String getTitle() {
           return title;
      }
      public void setTitle(String title) {
            this.title = title;
      }
      public String getMainTitle() {
            return mainTitle;
      }
      public void setMainTitle(String mainTitle) {
            this.mainTitle = mainTitle;
      }
      public String getBody() {
            return body;
      }
      public void setBody(String body) {
            this.body = body;
      }
      public String getSummary() {
            return summary;
      }
      public void setSummary(String summary) {
            this.summary = summary;
      }
}
صنع Main Class

package com.Xstream.NST;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class Main {
      public static void main(String[] args) throws IOException {          
            XStream xstream = new XStream(new StaxDriver());
            Blog blog = new Blog("Blog Title XStream",
"How To use XStream library ?",
"Blog Body","Summary");
           
            String xml = xstream.toXML(blog);
           
            Blog newBlog = (Blog) xstream.fromXML(print);
            System.out.println(newBlog.getTitle());
System.out.println(newBlog.getSummary());
      }
}