我有一个问题,我使用模式xsd和XJC生成了类。当我运行封送操作时,我会得到以下错误:
[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>
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;
* <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");
我有一个问题,我使用一个模式xsd和XJC生成了类。运行封送操作时,会出现以下错误: xsd代码的部分是: 并运行项目,我有这个错误。为什么?请帮帮我.谢谢
我正在尝试使用JAXB进行封送处理。 我的输出是这样的: ...但我需要这样的输出: 如果取消对代码的注释,则会得到。没有它,我可以编译,但我不能得到所需的精确输出。 我的豆子长这样: 适配器类
我想让具有多个内部类的抽象类对其进行扩展,并可以通过静态方法创建其内部类的实例,但我得到编译器错误“无法访问ITask类型的封闭实例。必须使用ITask类型的封闭实例限定分配(例如,x.new A(),其中x是ITask的实例)。” 我发现内部类应该由外部类的实例创建,但是我的外部类有抽象方法,我不能创建它的实例。我创建了扩展父类的内部类,因为我习惯于控制它们的创建。那么有没有办法让我的模式发挥作
问题内容: 我正在使用JAXB读写XML。我想要的是使用基本JAXB类进行编组,并使用继承的JAXB类进行编组。这是允许发送方Java应用程序将XML发送到另一个接收方Java应用程序。发送者和接收者将共享一个公共的JAXB库。我希望接收者将XML解组为接收者特定的JAXB类,该类扩展了通用JAXB类。 例: 这是发送方使用的常见JAXB类。 这是在解组XML时使用的特定于接收者的JAXB类。接收
我有一个由第三方供应商提供的XSD文件。我需要解析该XSD文件并生成Java对象。我使用JAXB通过maven插件解析XSD文件。 一切都很顺利,直到我最近要求使用来自正在解析的XML中的一个标记的数据。标记的complexType具有mixed=true,因此JAXB生成的java类如下所示。 XSD复杂类型: 生成的JAXB类 GeneralRemark>类不包含List ,而是包含List
我有以下代码: 我知道什么都不做,但我的Hello,World程序没有它也可以编译。只有我定义的类在我身上失败了。 它拒绝编译。我得到在创建新事物的行中。我猜: 我有系统级问题(在DrJava或我的Java安装中)或 我对如何用java构建工作程序有一些基本的误解 有什么想法吗?