XStream进行xml和bean互转

加入pom

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.3</version>
</dependency>
  1. 最简单的使用方式
XStream xStream = new XStream();
//声明XStream注解来源
xStream.processAnnotations(FuiouResponse.class);
//xml 转bean
Object o = xStream.fromXML(xmlStr);
//bean 转xml
xStream.toXML(response)
  1. 需要指明注解
/**
 * @author laoliangliang
 * @date 2018/11/6 17:00
 */
@XStreamAlias("ap")
public class FuiouResponse {

    @XStreamAlias("signature")
    private String signature;
    @XStreamAlias("plain")
    private Plain plain;

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getSignature() {
        return signature;
    }

    public Plain getPlain() {
        return plain;
    }

    public void setPlain(Plain plain) {
        this.plain = plain;
    }

    public class Plain {
        @XStreamAlias("resp_code")
        private String respCode;

        @XStreamAlias("resp_desc")
        private String respDesc;

        public String getRespCode() {
            return respCode;
        }

        public void setRespCode(String respCode) {
            this.respCode = respCode;
        }

        public String getRespDesc() {
            return respDesc;
        }

        public void setRespDesc(String respDesc) {
            this.respDesc = respDesc;
        }
    }
}

和jpa类似,取个别名

原文地址:https://www.cnblogs.com/sky-chen/p/9916957.html

时间: 2024-10-11 22:38:05

XStream进行xml和bean互转的相关文章

Xstream util xml 与 bean之间互转

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 st

java 用 jackson 进行 xml bean 互转

xml和bean互转 maven 依赖包 <properties> <json-version>2.4.2</json-version> </properties> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${json

xstream对象xml互转

1.引入jar包 xpp3_min-1.1.4c.jarxstream-1.4.8.jar 2.建立java bean package com.jdw.bean; import java.util.ArrayList; import java.util.List; public class Company { private List<Department> departments = new ArrayList<Department>(); public List<Depa

XStream解析xml和json

XStream是一个在我看来比较好的一个第三方包了.因为它在解析时支持注解.这样很是方便,并且xml跟json这两种格式的文件都能进行解析,XStream本属于java的一个第三方包,甚是好用,若是拿它在android开发环境,也是能正常解析,但有点美中不足,因为android开发环境时,XStream不太支持json转对象,只支持对象转json,其他xml与对象的互转都支持.这样的话双解析就有了那么一点瑕疵之处,不过话说回来,没多少需求的数据交互会用json跟xml切来切去的,当只是json转

xstream 实现xml和对象转换

1.jar包 xstream-1.4.2.jar     xpp3_min-1.1.4c.jar 2.工具类 package com.core.util; import java.lang.reflect.Field; import java.util.Iterator; import java.util.List; import java.util.Map; import com.core.model.Mobile; import com.thoughtworks.xstream.XStrea

Json、JavaBean、Map、XML之间的互转

思路是JavaBean.Map.XML都可以用工具类很简单的转换为Json,进而实现互相转换 1.Map.XML与Json互转 mvn依赖 <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20171018</version> </dependency> entity public class Stud

使用谷歌Gson实现Json串和Java Bean互转

/** * 使用谷歌Gson实现Json串和Java Bean互转 */ public class JsonHelper { public static String toJson(Object src){ return new Gson().toJson(src); } public static <T> T fromJson(String json, Class<T> clazz) throws Exception { return new Gson().fromJson(js

xstream读取xml

xStream可以Java和xml相互转换.下面主要说明xstream读取xml文件(和xstream将Java转化成xml差不多).本次学习用到的是xstream1.4.7 项目中使用到的xml文件 <config name="personconfig"> <!-- <add> --> <!-- 使用addImplicitCollection可以不出现 --> <address> <add>abc;abc<

Xstream annotion xml to java for selenium test

package com.dao; import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; @XStreamAlias("html") public class Html { public Head head