gpt4 book ai didi

java - JAXB 问题,Marshal,无法编码类型 "java.lang.String"

转载 作者:行者123 更新时间:2023-11-29 07:49:42 31 4
gpt4 key购买 nike

我有一个问题,我使用带有 xjc 的模式 xsd 生成了类。当我运行编码操作时,出现以下错误:

[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation]    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)  at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)    at prova.StampGenericXML.staticStampGenericXML(StampGenericXML.java:42)     at prova.TestLauncher.main(TestLauncher.java:199) Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an @XmlRootElement annotation     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)   at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(Unknown Source)  at com.sun.xml.internal.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(Unknown Source)  at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(Unknown Source)  at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty.serializeBody(Unknown Source)  at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(Unknown Source)    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)     at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)

xsd代码部分为:

<xsd:complexType name="formattedText">
<xsd:annotation>
<xsd:documentation>Formatted text according to parts of XHTML 1.1 </xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:any namespace="http://www.w3.org/1999/xhtml" processContents="lax"/>
</xsd:sequence>
</xsd:complexType>

FormattedText 类是:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;
import org.w3c.dom.Element;

/** * 根据 XHTML 1.1 的部分格式化文本 * *

用于格式化文本复杂类型的 Java 类。 * *

以下架构片段指定此类中包含的预期内容。 * *

 * <complexType name="formattedText"> *   <complexContent> *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> *       <sequence> *         <any processContents='lax' namespace='http://www.w3.org/1999/xhtml'/> *       </sequence> *     </restriction> *   </complexContent> * </complexType> * 
* * *

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "formattedText", propOrder = {
"any"
})
public class FormattedText {

@XmlAnyElement(lax = true)
protected Object any;

/**
* Gets the value of the any property.
*
* @return
* possible object is
* {@link Element }
* {@link Object }
*
*/
public Object getAny() {
return any;
}

/**
* Sets the value of the any property.
*
* @param value
* allowed object is
* {@link Element }
* {@link Object }
*
*/
public void setAny(Object value) {
this.any = value;
}

}

这是编码操作的类

public class StampGenericXML {  
static public void staticStampGenericXML(Object objectJAXB, String context) throws ParserConfigurationException, SAXException, TransformerException{
try {

JAXBContext jaxbLocalContext = JAXBContext.newInstance ("org.plcopen.xml.tc6_0201");

Marshaller marshaller = jaxbLocalContext.createMarshaller();
marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(objectJAXB, System.out);
} catch (JAXBException e1) {
e1.printStackTrace();
}


}

当我创建一个 FormattedText 对象时:

FormattedText text = new FormattedText();
text.setAny("table");

然后运行项目,我遇到了错误。为什么?请帮我。谢谢

最佳答案

尝试:

text.setAny(new JAXBElement<String>(
new QName("table"), String.class, "tableContent");

关于java - JAXB 问题,Marshal,无法编码类型 "java.lang.String",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205877/

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