簡體   English   中英

JAXB,Marshal的問題-無法封送類型“ java.lang.String”

[英]Problems with JAXB, Marshal, - unable to marshal type “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>

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

用於formattedText復雜類型的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>
 *
* * *

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


}

這是元帥作戰的班級

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM