java element string_使用Java将字符串转换为JAXBElement

在尝试将String对象转换为JAXBElement时遇到问题。创建了一个ObjectFactory,用JAXBElement封装了ApplicationIngestionRequest类的对象,并使用JAXBContext进行 marshalling 和 unmarshalling。然而,尝试将结果设置到方法setData中时,出现类型不匹配。日志显示XML输出但无法直接设置到JAXBElement字段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在将String对象转换为JAXBElement字符串对象时遇到了一些问题,我需要设置此对象

这是我需要设置值的目标方法

public void setData(JAXBElement value) {

this.data = ((JAXBElement ) value);

}对于这个,我写了类似这样的代码

ObjectFactory factory = new ObjectFactory();

JAXBElement jaxbElement = new JAXBElement(

new QName(ApplicationIngestionRequest.class.getSimpleName()), ApplicationIngestionRequest.class, request);

StringWriter writer = new StringWriter();

JAXBContext context = JAXBContext.newInstance(ApplicationIngestionRequest.class);

context.createMarshaller().marshal(jaxbElement, writer);

LOG.info("JAXBElement object :\n"+ writer.toString());

Unmarshaller u = context.createUnmarshaller();

JAXBElement o = (JAXBElement) u.unmarshal(new StringReader(writer));日志给我下面的输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>0000005511现在,当我尝试将方法设置为

losRequest.setData(o.toString());它不允许将其设置为JAXBElement格式。任何想法将不胜感激。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值