Xstream util xml 與 bean之間互轉

來源:互聯網
上載者:User

標籤: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之間互轉

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.