JAXB,元帅的问题 - 无法封送类型 "java.lang.String"



我有问题,我使用带有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>

类ForstattedText是:

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的部分格式化文本 * *

for for for forfertedText复杂类型的Java类。 * *

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

 *&lt; complextyType名称=" formattedText"> *&lt;复合> *&lt;限制基础=" {http://www.w3.org/2001/xmlschema} anytype"> *&lt;序列> *&lt;任何processContents ='lax'namespace ='http://www.w3.org/1999/xhtml'/> *&lt;/sequence> *&lt;/限制> *&lt;/comperivecontent> *&lt;/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 text = new FormattedText();
text.setAny("table");

运行该项目,我有错误。为什么?请帮我。谢谢

尝试:

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

相关内容

最新更新