Conversion of Java objects and XML

Source: Internet
Author: User
Tags stringbuffer
 
Package com.leiwei.test; 
Import java.util.ArrayList; 
Import Java.util.Iterator; 
  

Import java.util.List; 
Import Com.leiwei.model.Person; 
Import com.leiwei.model.Persons; 
  

Import Com.thoughtworks.xstream.XStream;  /** * * @author Leiwei * * Convert Java objects to XML or convert XML to Java objects * * need to be used to Xstream-1.3.jar, Xpp3_min- 1.1.4c.jar * */public class Test {public static Persons getpersons () {//Build person object, dynamic can be from the database 
      Remove person p = new person (1, "Leiwei", 22); 
  

      person P2 = new Person (2, "Jinlei", 22); 
      Put the object into the person set list<person> Listperson = new arraylist<person> (); 
      Listperson.add (P); 
Listperson.add (p2); 
      
      Constructs a persons object persons persons = new Persons (Listperson); 
   return persons;  
  

      public static void Main (string[] args) {//Get persons object Persons persons = Getpersons (); Give each entity class an alias, as the root tag for the current entity XStReam stream = new XStream (); 
      Stream.alias ("Persons", persons. Class); 
        
      Stream.alias ("person", person. Class); 
      Converts a Java object into an XML String obj2xml = stream.toxml (persons); System. 
      
      Out. println (Obj2xml.tostring ()); 
      
     Convert XML to Java object Persons xml2obj = (Persons) stream.fromxml (obj2xml); for (Iterator iterator = Xml2obj.getlistperson (). iterator (); Iterator.hasnext ();) 
         
         {Person person = (person) iterator.next (); System. 
      Out. println (Person.getidcard () + "--" +person.getname () + "--" +person.getage ()); 
 
 } 
      
   } 
}
 package Com.leiwei.model; 
   public class Person {private int idcard; 
   private String name; 
   
   private int age; 
      Public person () {} public person (int idcard, String name, int age) {super (); this. 
      Idcard = Idcard; this. 
      name = name; this. 
   Age = age; 
   public int Getidcard () {return idcard; 
   public void Setidcard (int idcard) {this. Idcard = Idcard; 
   Public String GetName () {return name; 
   public void SetName (String name) {this. name = name; 
   public int getage () {return age; 
   public void Setage (int age) {this. age = age;  
              @Override public String toString () {return "person{" + "idcard= '" + idcard + ' \ "+ 
   ", Name= '" + name + ' \ ' + ", age= '" + Age + ' \ "+"}\n "; } 
} 

Package Com.leiwei.model; 
  

Import Java.util.Iterator; 
Import java.util.List; 
  

public class Persons { 
  

   private list<person> Listperson; 
  

   Public Persons () {} public 
  

   Persons (list<person> listperson) { 
      super (); 
      this. Listperson = Listperson; 
   } 
   Public list<person> Getlistperson () {return 
      Listperson; 
   } 
  

   public void Setlistperson (list<person> listperson) {this 
      . Listperson = Listperson; 
   } 
@Override public 
   String toString () { 
      
      stringbuffer sb= new StringBuffer (); 
      For (iterator it= Listperson. Iterator (), it.hasnext ();) {person p= () 
         ; 
         Sb.append (It.tostring ()); 
      } 
      Return "persons{" + 
            "listperson=" + sb.tostring () + 
            "}\n"; 
  

   } 
} 
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.