使用Java将字符串转换为JAXBElement
我在将String对象转换为JAXBElement String对象时遇到了一些问题,我需要在其中设置这个 这是我需要设置值的目标方法使用Java将字符串转换为JAXBElement,java,jaxbelement,Java,Jaxbelement,我在将String对象转换为JAXBElement String对象时遇到了一些问题,我需要在其中设置这个 这是我需要设置值的目标方法 public void setData(JAXBElement<String> value) { this.data = ((JAXBElement<String> ) value); } 它不允许我将其设置为预期的JAXBElement格式。任何想法都将不胜感激 根据代码片段[setData(JAXBElement值)],se
public void setData(JAXBElement<String> value) {
this.data = ((JAXBElement<String> ) value);
}
它不允许我将其设置为预期的JAXBElement格式。任何想法都将不胜感激 根据代码片段[setData(JAXBElement值)],setData,接受“(JAXBElement”)的实例。但是在这里,您试图设置一个字符串值[losRequest.setData(o.toString())]。这里您必须设置一个“JAXBElement”实例。这可能就是问题所在。1)什么是
o
?2) 为什么要将value
强制转换为声明它的同一类型(这是不必要的)?3) 对任何对象调用toString()
,都会返回一个。。。嗯,它返回一个字符串,而不是JAXBElement
。你到底想达到什么目的?4) “它不允许我。”你犯了什么错误?@Seelenvirtuose我已经更新了问题。。我只想将该XML输出转换为JAXBElement字符串,以便在该方法中设置它。您能告诉我如何将该XML对象转换为JAXBElement字符串对象吗?请尝试这个新的JAXBElement(o.toString());
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));
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ApplicationIngestionRequest><BranchCode></BranchCode><SourceCode>0000005511</SourceCode></ApplicationIngestionRequest>
losRequest.setData(o.toString());