1、JAXB生成XML简单用法

构建对象

公共请求对象

@Data
@XmlRootElement(name = "CommonExampleReq")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
        "commonReq"
})
public class CommonExampleReq implements Serializable {
    /**
     * 公共请求参数
     */
    private CommonReq commonReq;
}

公共对象

@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "CommonReq")
@XmlType(propOrder = {
        "serialNum", "reqTime", "reqParam"
})
@XmlSeeAlso({ExampleReq.class})
public class CommonReq<T> {
    /**
     * 序列号
     */
    private String serialNum;
    /**
     * 请求时间
     */
    private String reqTime;
    /**
     * 请求内容
     */
    @XmlAnyElement(lax = true)
    private T reqParam;
}

请求对象

@Data
@XmlRootElement(name = "ExampleReq")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {
        "reqParam1", "reqParam2"
})
public class ExampleReq {
    /**
     * 请求参数1
     */
    private String reqParam1;
    /**
     * 请求参数2
     */
    private String reqParam2;
}

对象转XML输出

public class Converter {
    public static String convertXml(Object obj) {
        // 输出流
        StringWriter sw = new StringWriter();
        try {
            // JAXB实现转换
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            //设置编码UTF-8
            marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
            // 格式化xml输出
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}

自定义XML声明

public class Converter {
    public static String convertXml(Object obj) {
        // 输出流
        StringWriter sw = new StringWriter();
        try {
            // JAXB实现转换
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            //设置编码UTF-8
            marshaller.setProperty(Marshaller.JAXB_ENCODING,"UTF-8");
            // 格式化xml输出
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.FALSE);
            // 自定义声明
            sw.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
}

生成XML

public String entityToXml(Object obj) {
        CommonExampleReq data = new CommonExampleReq();
        CommonReq<ExampleReq> commonReq = new CommonReq<>();
        commonReq.setReqParam(obj);
        commonReq.setSerialNo(String.valueOf(Math.round(Math.random() * 100)) + System.currentTimeMillis());
        commonReq.setReqTime(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        data.setCommonReq(commonReq);
        //生成XML字符串
        return converter.convertXml(data);
    }

请求包实例

<?xml version=“1.0” encoding=“UTF-8”?>
<CommonExampleReq>
    <CommonReq>
        <serialNum>******</serialNum>
        <reqTime>********</reqTime>
        <reqParam>
            <reqParam1>请求参数1</reqParam1>
            <reqParam2>请求参数2</reqParam2>
        </reqParam>
    </CommonReq>
</CommonExampleReq>

本篇为JAXB简单实例,如有不足,欢迎指出。

原创不易,转载、引用请标明出处。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用JAXB生成XML时,如果需要动态生成`xmlns:xsi`属性,可以使用`javax.xml.bind.Marshaller.JAXB_SCHEMA_LOCATION`属性来指定XML Schema的位置和命名空间,并通过`javax.xml.bind.Marshaller.setProperty()`方法将该属性设置为要生成XML中的`xmlns:xsi`属性的值。例如: ``` JAXBContext jaxbContext = JAXBContext.newInstance(Root.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://example.com/ns path/to/schema.xsd"); marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() { @Override public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) { if ("http://www.w3.org/2001/XMLSchema-instance".equals(namespaceUri)) { return "xsi"; } return null; } }); Root root = new Root(); marshaller.marshal(root, System.out); ``` 在这个例子中,`Marshaller.JAXB_SCHEMA_LOCATION`属性指定了XML Schema的位置和命名空间。`com.sun.xml.bind.namespacePrefixMapper`属性用于将`http://www.w3.org/2001/XMLSchema-instance`命名空间映射到`xsi`前缀。在生成XML时,JAXB会自动将`xmlns:xsi`属性添加到生成XML中,并将其值设置为`http://www.w3.org/2001/XMLSchema-instance`。例如: ``` <root xmlns="http://example.com/ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.com/ns path/to/schema.xsd"> ... </root> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值