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
时间: 2024-10-16 03:36:25