Imported package
Jettison-1.0.1.jar
Xpp3_min-1.1.4c.jar
Xstream-1.3.1.jar
Userinfo:
package com.lb; public class userInfo { private String name; private String phoneNumber; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Name : "+this.name+ "\nPhoneNumber : "+this.phoneNumber+ "\nAddress : "+this.address; } }
Userinfo2xml:
Package COM. lb; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import COM. thoughtworks. xstream. xstream; import COM. user. userinfo; public class userinfo2xml {public static void main (string [] ARGs) {// initialize bean object userinfo = new userinfo (); userinfo. setname ("XXX"); userinfo. setphonenumber ("XXXXXXXXX"); userinfo. setaddress ("Changping District, Beijing"); // initialize the xstream object xstream = ne W xstream (); // xstream. omitfield (employee. class, "name"); // defines the value of an attribute without XML serialization. // Xstream. useattributefor (string. class); // defines all string fields as attribute tag display // xstream. useattributefor ("name", String. class); // For All string-type field names to name // defined as attribute tag display, this value cannot be obtained during reading try {fileoutputstream FS = new fileoutputstream ("D: /userinfo. XML "); xstream. toxml (userinfo, FS);} catch (filenotfoundexception E1) {e1.printstacktrace ();}}}