標籤:des blog http io ar java sp div on
package com.demo.util;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.mapper.MapperWrapper;public class XmlUtil { /** * java 轉換成xml * @Title: toXml * @Description: TODO * @param obj 對象執行個體 * @return String xml字串 */ public static String toXml(Object obj){ XStream xstream=new XStream(); xstream.processAnnotations(obj.getClass()); //通過註解方式的,一定要有這句話 return xstream.toXML(obj); } /** * 將傳入xml文本轉換成Java對象 * @Title: toBean * @Description: TODO * @param xmlStr * @param cls xml對應的class類 * @return T xml對應的class類的執行個體對象 * * 調用的方法執行個體:PersonBean person=XmlUtil.toBean(xmlStr, PersonBean.class); */ public static <T> T toBean(String xmlStr,Class<T> cls){ XStream xstream = new XStream() {
//忽略xml中的未知節點 @Override protected MapperWrapper wrapMapper(MapperWrapper next) { return new MapperWrapper(next) { @SuppressWarnings("rawtypes")@Override public boolean shouldSerializeMember(Class definedIn, String fieldName) { if (definedIn == Object.class) { return false; } return super.shouldSerializeMember(definedIn, fieldName); } }; } }; xstream.processAnnotations(cls); @SuppressWarnings("unchecked")T obj=(T)xstream.fromXML(xmlStr); return obj; } }
Person.java
package com.demo.bean;import java.util.List;import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("Person")public class Person {private String userName;private List<Book> bookList;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public List<Book> getBookList() {return bookList;}public void setBookList(List<Book> bookList) {this.bookList = bookList;}}
Book.java
package com.demo.bean;import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("book")public class Book {private String bookName;private int price;public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public int getPrice() {return price;}public void setPrice(int price) {this.price = price;}public Book(String bookName, int price) {super();this.bookName = bookName;this.price = price;}}
TestMain.java
package com.demo.main;import java.util.ArrayList;import java.util.List;import com.demo.bean.Book;import com.demo.bean.Person;import com.demo.util.XmlUtil;public class TestMain {public static void main(String[] args) {//testBeanToXml();testXmlToBean();}public static void testBeanToXml(){Person p = new Person();p.setUserName("xtream");List<Book> bookList =new ArrayList<Book>();Book book = new Book("呵呵",15);bookList.add(book);p.setBookList(bookList);String xml=XmlUtil.toXml(p);System.out.println(xml);}public static void testXmlToBean(){String xml ="<Person><userName>xstream</userName><age>25</age><bookList><book><bookName>呵呵</bookName><price>15</price></book></bookList></Person>";Person p =XmlUtil.toBean(xml, Person.class);System.out.println("p.getUserName():"+p.getUserName());System.out.println("p.getBookList().get(0).getBookName():"+p.getBookList().get(0).getBookName());}}
xstream 官方文檔 http://xstream.codehaus.org/manual-tweaking-output.html
Xstream util xml 與 bean之間互轉