对象实体:
package cn.framework.t3; import java.util.ArrayList; import java.util.Date; import java.util.List; public class User { private int userId; private String userName; private String pwd; private List<UserLog> logs=new ArrayList<UserLog>(); private Date lastVisted; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public List getLogs() { return logs; } public void setLogs(List logs) { this.logs = logs; } public Date getLastVisted() { return lastVisted; } public void setLastVisted(Date lastVisted) { this.lastVisted = lastVisted; } @Override public String toString() { return "User [userId=" + userId + ",userName=" + userName + ",pwd=" + pwd + ",logs=" + logs + ",lastVisted=" + lastVisted + "]"; } }
package cn.framework.t3; public class UserLog { private int logId; private String ip; public int getLogId() { return logId; } public void setLogId(int logId) { this.logId = logId; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } @Override public String toString() { return "UserLog [logId=" + logId + ",ip=" + ip + "]"; } }
XStreamUtils
package cn.framework.t3; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PrintWriter; import java.util.Locale; import org.apache.log4j.Logger; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.xml.DomDriver; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; public class XStreamUtils { private static final Logger logger=Logger.getLogger("cn.framework.t1.XStreamUtils"); private static XStream xstream; static{ logger.info("init xstream ..."); //创建基于流非Dom方式的解析器来提供性能 xstream=new XStream(); /*******对生成的xml做一些设置*******/ //设置别名,不设置将采用完全类名 xstream.alias("User",User.class); xstream.alias("UserLog",UserLog.class); //设置类成员别名 xstream.aliasField("id",User.class,"userId"); //类的属性默认为xml的元素,现在修改为xml属性 xstream.aliasAttribute(UserLog.class,"logId","lid"); xstream.useAttributeFor(UserLog.class,"logId"); //只保留集合内元素,去掉上级标签 xstream.addImplicitCollection(User.class,"logs"); //注册自定义转换器 xstream.registerConverter(new DateConverter(Locale.SIMPLIFIED_CHINESE)); } public static void objectToXML(User user) throws IOException{ logger.info("do objectToXML ..."); //创建一个PrintWriter对象用于输出 PrintWriter pw = new PrintWriter("e:/out/xstream_user.xml"); //选用一个HierarchicalStreamWriter实现类创建输出 PrettyPrintWriter ppw = new PrettyPrintWriter(pw); //创建对象输出流 ObjectOutputStream out = xstream.createObjectOutputStream(ppw); out.writeObject(user); out.close(); } public static void xmlToObject() throws IOException,ClassNotFoundException{ logger.info("do xmlToObject ..."); //通过对象流进行输入操作 FileReader fileReader = new FileReader("e:/out/xstream_user.xml"); BufferedReader bufferedReader = new BufferedReader(fileReader); //创建对象输入流 ObjectInputStream in = xstream.createObjectInputStream(bufferedReader); User user = (User) in.readObject(); System.out.println(user); } }
xstream=new XStream(); 这里创建XML解析器的方式与之前有所不同,之前是基于DOM方式创建的,现在是基于流方式创建的。
测试类:
package cn.framework.t3; import java.io.FileNotFoundException; import java.io.IOException; import java.text.ParseException; import java.util.Date; public class Test { public static void main(String[] args) throws ParseException,IOException,ClassNotFoundException { // User user = new User(); // user.setLastVisted(new Date()); // user.setPwd("123456"); // user.setUserId(1); // user.setUserName("aaa"); // // // UserLog userLog = new UserLog(); // userLog.setIp("127.0.0.1"); // userLog.setLogId(1); // user.getLogs().add(userLog); // // UserLog userLog1 = new UserLog(); // userLog1.setIp("127.0.0.1"); // userLog1.setLogId(2); // user.getLogs().add(userLog1); // // XStreamUtils.objectToXML(user); XStreamUtils.xmlToObject(); } }原文链接:https://www.f2er.com/xml/293584.html