zoukankan      html  css  js  c++  java
  • XStream 快速转换xml

    项目地址:http://xstream.codehaus.org/tutorial.html

    (以下来源于官网)

    1、Create classes to be serialized(初始化类)

    public class Person {
      private String firstname;
      private String lastname;
      private PhoneNumber phone;
      private PhoneNumber fax;
      // ... constructors and methods
    }
    
    public class PhoneNumber {
      private int code;
      private String number;
      // ... constructors and methods
    }

    2、Initializing XStream(初始化XStream)

    XStream xstream = new XStream();
    XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
    XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
    xstream.alias("person", Person.class);
    xstream.alias("phonenumber", PhoneNumber.class);

    3、Serializing an object to XML(转换为xml例子)

    Person joe = new Person("Joe", "Walnes");
    joe.setPhone(new PhoneNumber(123, "1234-456"));
    joe.setFax(new PhoneNumber(123, "9999-999"));
    String xml = xstream.toXML(joe);
    <person>
      <firstname>Joe</firstname>
      <lastname>Walnes</lastname>
      <phone>
        <code>123</code>
        <number>1234-456</number>
      </phone>
      <fax>
        <code>123</code>
        <number>9999-999</number>
      </fax>
    </person>

    4、Deserializing an object back from XML(xml逆转)

    Person newJoe = (Person)xstream.fromXML(xml);
  • 相关阅读:
    vue 快速开发
    java 查es
    es filter 的使用
    es查询例子
    es的基本查询
    linux top命令VIRT,RES,SHR,DATA的含义
    Redis和MC的对比
    决TIME_WAIT过多造成的问题
    MariaDB yum 安装
    more 命令相关
  • 原文地址:https://www.cnblogs.com/liqw/p/3519445.html
Copyright © 2011-2022 走看看