简体   繁体   English

包装一个JAXBElement实例的字符串

[英]Wrap a String of an instance of JAXBElement

I have the following code, 我有以下代码,

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);

in which I am trying to wrap the Java String Class as a jaxb element , since if I do not I get the 在这种情况下,我试图将Java String类包装为jaxb element ,因为如果不这样做,我会得到

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

However when I try wrap the java.lang.string in a jaxb element , I get the following error 但是,当我尝试将java.lang.string包装在jaxb element ,出现以下错误

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

The setUuid method looks as follows setUuid方法如下所示

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

How can I then wrap my String as a jaxb element where this error will not be thrown? 然后,如何将我的String包装为一个不会抛出此错误的jaxb element

When marshalled as the root object an instance of String needs to be wrapped in a JAXBElement to provide the root element information (same as for any class without an @XmlRootElement annotation. 当作为根对象编组时,需要将String的实例包装在JAXBElement以提供根元素信息(与没有@XmlRootElement批注的任何类相同)。

When marshalled as a field or property value the containing element is normally derived from the containing field/property so a JAXBElement by default is not required. 当作为字段或属性值编组时,包含元素通常从包含字段/属性派生,因此默认情况下不需要JAXBElement

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

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