繁体   English   中英

使用 Java 将字符串转换为 JAXBElement

[英]Convert string to JAXBElement using Java

我在将 String object 转换为 JAXBElement 字符串 object 时遇到了一些问题,我需要在其中设置这个

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

public void setData(JAXBElement<String> value) {
    this.data = ((JAXBElement<String> ) value);
}

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

 ObjectFactory factory = new ObjectFactory();
    JAXBElement<ApplicationIngestionRequest> 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<ApplicationIngestionRequest> o = (JAXBElement<ApplicationIngestionRequest>) u.unmarshal(new StringReader(writer));

日志给了我关注 output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ApplicationIngestionRequest><BranchCode></BranchCode><SourceCode>0000005511</SourceCode></ApplicationIngestionRequest>

现在,当我尝试将方法设置为

losRequest.setData(o.toString());

它不允许我将其设置为 JAXBElement 格式。 任何想法将不胜感激。

根据代码片段 [setData(JAXBElement value)],setData,接受 '(JAXBElement') 的实例。 但是在这里,您尝试设置一个字符串值 [losRequest.setData(o.toString())]。 在这里,您必须设置一个“JAXBElement”实例。这可能是问题所在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM