gpt4 book ai didi

java - 包装 JAXBElement 实例的字符串

转载 作者:行者123 更新时间:2023-12-01 12:05:53 25 4
gpt4 key购买 nike

我有以下代码,

JAXBContext jc = JAXBContext.newInstance(TestResults.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
OutputStream os = new FileOutputStream( "nosferatu.xml" );
TestResults tr = new TestResults();
tr.setUuid(new JAXBElement<String>(new QName("uuid"),String.class, "uuidcontent"));
//ObjectFactory objectFactory = new ObjectFactory();
m.marshal(new JAXBElement<TestResults>(new QName("uri","local"),TestResults.class,new TestResults()), System.out);

我试图将Java String Class包装为jaxb element ,因为如果我不这样做,我就会得到

unable to marshal type “java.lang.String”错误。

但是当我尝试包装 java.lang.string 时在 jaxb element ,我收到以下错误

The method setUuid(String) in the type TestResults is not applicable for the arguments (JAXBElement<String>)

setUuid方法如下

public void setUuid(java.lang.String value) {
this.uuid = value;
}

我该如何包装我的 String作为jaxb element哪里不会抛出这个错误?

最佳答案

当编码为根对象时,String 的实例需要包装在 JAXBElement 中以提供根元素信息(与任何没有 JAXBElement 的类相同) >@XmlRootElement 注释。

当编码为字段或属性值时,包含元素通常从包含字段/属性派生,因此默认情况下不需要 JAXBElement

关于java - 包装 JAXBElement 实例的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27605000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com