zoukankan      html  css  js  c++  java
  • Java bean 转为xml

    Java bean 转为xml可以采用XStream类来完成

    pom.xml

    <dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.9</version>
    </dependency>

    public class BaseMessage {
      @XStreamAliasC"ToUserName")
      private String toUserName;
      @XStreamAlias(" FromUserName")
      private String fromUserName ;
      @XStreamAliasC"CreateTime")
      private String createTime ;
      @XStreamAliasC"MsgType")
      private String msgType;

           get.set...

    }

    @XStreamAlias("xml")
    public class TextMessage extends BaseMessage {
      @XStreamAliasC"Content")
      private String content;

          get.set...

    }

    测试代码:

    XStream stream = new XStream();
    //
    stream. processAnnotations(TextMessage. class);
    String xml = stream. toXML(tm); //tm是TextMessage类对象
    System. out. println(xml );

     输出结果:

    <xml>
      <ToUserName>from</ToUserName>
      <FromUserName>to</FromUserName>
      <CreateTime>1540898036</CreateTime>
      <MsgType>text </MsgType>
      <Content>还好</Content>
    </xml>

  • 相关阅读:
    django 设置局域网内访问项目
    104. Maximum Depth of Binary Tree
    multi dimension array
    Longest Plindrome Subarray
    BlockQueue
    H2O
    HoppingIterator
    midStack
    Reverse Words in a String II
    public boolean canPlaceFlowers(List<Boolean> flowerbed, int numberToPlace)
  • 原文地址:https://www.cnblogs.com/hwgok/p/12527708.html
Copyright © 2011-2022 走看看