当前位置: 首页 > 知识库问答 >
问题:

试图封送丢失@XmlRootelement的JAXB根对象时出错

皮安顺
2023-03-14
    null

我尝试在自定义绑定文件中向有问题的类添加注释,但它将注释添加到类以及getContent()方法中,该类将无法编译。我不认为我应该添加这个注释,因为这是一个复杂的类型,是根的一个组件,实际上它嵌套在树中相当深的地方。我已经读到可以将 标记添加到全局绑定元素中,但是我已经将许多属性添加到全局绑定元素中,当我这样做时,它将无法编译。

有人能帮我指出正确的方向吗?这可能是我构建JAXB对象的方式中的错误吗?我已经在调试器中进行了详细说明,当来自com.sun.xml.bind.v2.runtime.property包的ArrayReferenceNodeProperty类试图序列化列表主体时,当它获得这些类的JaxBeanInfo时,没有dom处理程序集,并且jaxbType不是对象,因此它认为它需要是根。

我没有在基于web的服务中使用这一点,除了在绑定文件中解决命名冲突的调整之外,还没有创建任何自定义类或绑定。

下面是顶层架构文件:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xs:annotation>
    <xs:documentation>
        *****************************************************************************
        *                                                                           *
        *                        S1000D Issue 3                                     *
        *                          XML Schema                                       *
        *                                                                           *
        *                     Release Version 3.0                                   *
        *                       Date: 2007-07-31                                    *
        *                                                                           *
        *                                                                           *
        * The following attribute definition on the root element may be used to     *
        * call this set of declarations:-                                           *
        *                                                                           *
        * xsi:noNamespaceSchemaLocation=                                            *
        * "http://www.s1000d.org/S1000D_3-0/xml_schema_master/dm/procedSchema.xsd"  *
        *****************************************************************************
    </xs:documentation>
</xs:annotation>
<xs:redefine schemaLocation="elementGroups.xsd">
    <xs:group name="APPLIC">
        <xs:choice>
            <xs:sequence>
                <xs:element ref="displaytext"/> 
                <xs:choice minOccurs="0">
                    <xs:element ref="assert"/>
                    <xs:element ref="evaluate"/>
                </xs:choice>
            </xs:sequence>
            <xs:choice>
                <xs:element ref="assert"/>
                <xs:element ref="evaluate"/>
            </xs:choice>
        </xs:choice>
    </xs:group>
    <xs:group name="NPAR">
        <xs:sequence>
            <xs:group ref="WC"/>
            <xs:group ref="NPFFT" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="NPFFT">
        <xs:choice>
            <xs:group ref="NP"/>
            <xs:group ref="FFT"/>
        </xs:choice>
    </xs:group>
    <xs:group name="paracon">
        <xs:choice>
            <xs:group ref="text"/>
            <xs:group ref="list"/>
        </xs:choice>
    </xs:group>
    <xs:group name="text">
        <xs:choice>
            <xs:element ref="ein"/>
            <xs:element ref="cb"/>
            <xs:element ref="parasigdata"/>
            <xs:element ref="quantity"/>
            <xs:element ref="xref"/>
            <xs:element ref="indxflag"/>
            <xs:element ref="change"/>
            <xs:element ref="emphasis"/>
            <xs:element ref="symbol"/>
            <xs:element ref="subscrpt"/>
            <xs:element ref="supscrpt"/>
            <xs:element ref="refdm"/>
            <xs:element ref="reftp"/>
            <xs:element ref="ftnote"/>
            <xs:element ref="ftnref"/>
            <xs:element ref="acronym"/>
            <xs:element ref="acroterm"/>
            <xs:element ref="capgrp"/>
        </xs:choice>
    </xs:group>
    <xs:group name="TEXT-NOREFS">
        <xs:choice>
            <xs:element ref="xref"/>
            <xs:element ref="indxflag"/>
            <xs:element ref="symbol"/>
            <xs:element ref="subscrpt"/>
            <xs:element ref="supscrpt"/>
            <xs:element ref="ftnref"/>
            <xs:element ref="acronym"/>
            <xs:element ref="acroterm"/>
        </xs:choice>
    </xs:group>
    <xs:group name="STEP1">
        <xs:sequence>
            <xs:element ref="step1"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP2">
        <xs:sequence>
            <xs:element ref="step2"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP3">
        <xs:sequence>
            <xs:element ref="step3"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP4">
        <xs:sequence>
            <xs:element ref="step4"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP5">
        <xs:sequence>
            <xs:element ref="step5"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP6">
        <xs:sequence>
            <xs:element ref="step6"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP7">
        <xs:sequence>
            <xs:element ref="step7"/>
        </xs:sequence>
    </xs:group>
    <xs:group name="STEP8">
        <xs:sequence>
            <xs:element ref="step8"/>
        </xs:sequence>
    </xs:group>
</xs:redefine>
<xs:redefine schemaLocation="complexTypes.xsd">
    <xs:complexType name="contentType">
        <xs:complexContent>
            <xs:restriction base="contentType">
                <xs:sequence>
                    <xs:element ref="refs" minOccurs="0"/>
                    <xs:element ref="proced"/>
                </xs:sequence>
                <xs:attribute ref="id"/>
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:redefine>
<xs:element name="dmodule" type="dmoduleType"/>

对于xrefType,我只设置了两个属性,对于refdmType,我只设置了ALLDMC组中的另一个复杂类型。错误的两个复杂类型的代码是:

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" elementFormDefault="qualified" attributeFormDefault="qualified">
    <xs:import namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#" schemaLocation="rdf.xsd"/>
    <xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/>
    <xs:include schemaLocation="attributeGroups.xsd"/>
    <xs:include schemaLocation="simpleElements.xsd"/>
    <xs:include schemaLocation="complexElements.xsd"/>
    <xs:include schemaLocation="elementGroups.xsd"/>
    <xs:complexType name="refdmType">
        <xs:choice>
            <xs:sequence>
                <xs:element ref="applic" minOccurs="0"/>
                <xs:element ref="dmcextension" minOccurs="0"/>
                <xs:group ref="ALLDMC"/>
                <xs:element ref="issno" minOccurs="0"/>
                <xs:element ref="dmtitle" minOccurs="0"/>
                <xs:element ref="language" minOccurs="0"/>
            </xs:sequence>
            <xs:group ref="xlink:XLINKEXT" minOccurs="0" maxOccurs="unbounded"/>
        </xs:choice>
        <xs:attribute ref="target"/>
        <xs:attribute ref="refapplic"/>
        <xs:attributeGroup ref="bodyatt"/>
        <xs:attributeGroup ref="cntlcontent"/>
        <xs:attributeGroup ref="xlink:XLINKATT"/>
    </xs:complexType>
    <xs:complexType name="xrefType" mixed="true">
        <xs:sequence>
            <xs:element ref="applic" minOccurs="0"/>
            <xs:group ref="SPARCON" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute ref="xrefid"/>
        <xs:attribute ref="xidtype"/>
        <xs:attribute ref="target"/>
        <xs:attribute ref="destitle"/>
        <xs:attribute ref="pretext"/>
        <xs:attribute ref="posttext"/>
        <xs:attribute ref="refapplic"/>
        <xs:attributeGroup ref="cntlcontent"/>
        <xs:attributeGroup ref="xlink:XLINKATT3"/>
    </xs:complexType>
    </xs:schema>

下面是JAXB生成的类的主要部分。为了节省空间,我删除了大部分getter和setter:refdmtype:

/**
 * <p>Java class for refdmType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="refdmType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;choice>
 *         &lt;sequence>
 *           &lt;element ref="{}applic" minOccurs="0"/>
 *           &lt;element ref="{}dmcextension" minOccurs="0"/>
 *           &lt;group ref="{}ALLDMC"/>
 *           &lt;element ref="{}issno" minOccurs="0"/>
 *           &lt;element ref="{}dmtitle" minOccurs="0"/>
 *           &lt;element ref="{}language" minOccurs="0"/>
 *         &lt;/sequence>
 *         &lt;group ref="{http://www.w3.org/1999/xlink}XLINKEXT" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/choice>
 *       &lt;attGroup ref="{http://www.w3.org/1999/xlink}XLINKATT"/>
 *       &lt;attGroup ref="{}cntlcontent"/>
 *       &lt;attGroup ref="{}bodyatt"/>
 *       &lt;attribute ref="{}target"/>
 *       &lt;attribute ref="{}refapplic"/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "refdmType", propOrder = {
    "applicAndDmcextensionAndAge"
})
public class RefdmType implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElements({
        @XmlElement(name = "applic", type = ApplicType.class),
        @XmlElement(name = "dmcextension", type = DmcextensionType.class),
        @XmlElement(name = "age", type = AgeType.class),
        @XmlElement(name = "avee", type = AveeType.class),
        @XmlElement(name = "issno", type = IssnoType.class),
        @XmlElement(name = "dmtitle", type = DmtitleType.class),
        @XmlElement(name = "language", type = LanguageType.class),
        @XmlElement(name = "resource", namespace = "http://www.w3.org/1999/xlink", type = Resource.class),
        @XmlElement(name = "locator", namespace = "http://www.w3.org/1999/xlink", type = Locator.class),
        @XmlElement(name = "arc", namespace = "http://www.w3.org/1999/xlink", type = Arc.class)
    })
    protected List<Serializable> applicAndDmcextensionAndAge;
    @XmlAttribute(name = "target")
    protected String target;
    @XmlAttribute(name = "refapplic")
    @XmlIDREF
    @XmlSchemaType(name = "IDREF")
    protected Object refapplic;
    @XmlAttribute(name = "type", namespace = "http://www.w3.org/1999/xlink")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String type;
    @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink")
    protected String href;
    @XmlAttribute(name = "title", namespace = "http://www.w3.org/1999/xlink")
    protected String title;
    @XmlAttribute(name = "show", namespace = "http://www.w3.org/1999/xlink")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String show;
    @XmlAttribute(name = "actuate", namespace = "http://www.w3.org/1999/xlink")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    protected String actuate;
    @XmlAttribute(name = "authname")
    protected String authname;
    @XmlAttribute(name = "authdoc")
    protected String authdoc;
    @XmlAttribute(name = "id")
    @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
    @XmlID
    @XmlSchemaType(name = "ID")
    protected String id;
    @XmlAttribute(name = "level")
    @XmlSchemaType(name = "nonNegativeInteger")
    protected BigInteger level;
    @XmlAttribute(name = "mark")
    @XmlSchemaType(name = "nonNegativeInteger")
    protected BigInteger mark;
    @XmlAttribute(name = "change")
    protected ChangeType change;
    @XmlAttribute(name = "rfc")
    protected String rfc;

    /**
     * Gets the value of the applicAndDmcextensionAndAge property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the applicAndDmcextensionAndAge property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getApplicAndDmcextensionAndAge().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link ApplicType }
     * {@link DmcextensionType }
     * {@link AgeType }
     * {@link AveeType }
     * {@link IssnoType }
     * {@link DmtitleType }
     * {@link LanguageType }
     * {@link Resource }
     * {@link Locator }
     * {@link Arc }
     * 
     * 
     */
    public List<Serializable> getApplicAndDmcextensionAndAge() {
        if (applicAndDmcextensionAndAge == null) {
            applicAndDmcextensionAndAge = new ArrayList<Serializable>();
        }
        return this.applicAndDmcextensionAndAge;
    }
    }
 /**
 * <p>Java class for xrefType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within    this class.
 * 
 * <pre>
 * &lt;complexType name="xrefType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element ref="{}applic" minOccurs="0"/>
 *         &lt;group ref="{}SPARCON" maxOccurs="unbounded" minOccurs="0"/>
 *       &lt;/sequence>
 *       &lt;attGroup ref="{}cntlcontent"/>
 *       &lt;attGroup ref="{http://www.w3.org/1999/xlink}XLINKATT3"/>
 *       &lt;attribute ref="{}xrefid"/>
 *       &lt;attribute ref="{}xidtype"/>
 *       &lt;attribute ref="{}target"/>
 *       &lt;attribute ref="{}destitle"/>
 *       &lt;attribute ref="{}pretext"/>
 *       &lt;attribute ref="{}posttext"/>
 *       &lt;attribute ref="{}refapplic"/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "xrefType", propOrder = {
"content"
})
public class XrefType implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElementRefs({
      @XmlElementRef(name = "supscrpt", type = JAXBElement.class),
      @XmlElementRef(name = "subscrpt", type = JAXBElement.class),
      @XmlElementRef(name = "applic", type = JAXBElement.class)
    })
   @XmlMixed
   protected List<Serializable> content;
   @XmlAttribute(name = "xrefid")
   @XmlJavaTypeAdapter(Adapter5 .class)
   @XmlSchemaType(name = "IDREF")
   protected String xrefid;
   @XmlAttribute(name = "xidtype")
   protected XidtypeType xidtype;
   @XmlAttribute(name = "target")
   protected String target;
   @XmlAttribute(name = "destitle")
   protected String destitle;
   @XmlAttribute(name = "pretext")
   protected String pretext;
   @XmlAttribute(name = "posttext")
   protected String posttext;
   @XmlAttribute(name = "refapplic")
   @XmlIDREF
   @XmlSchemaType(name = "IDREF")
   protected Object refapplic;
   @XmlAttribute(name = "authname")
   protected String authname;
   @XmlAttribute(name = "authdoc")
   protected String authdoc;
   @XmlAttribute(name = "type", namespace = "http://www.w3.org/1999/xlink")
   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
   protected String type;
   @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink")
   protected String href;
   @XmlAttribute(name = "title", namespace = "http://www.w3.org/1999/xlink")
   protected String title;
   @XmlAttribute(name = "show", namespace = "http://www.w3.org/1999/xlink")
   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
   protected String show;
   @XmlAttribute(name = "actuate", namespace = "http://www.w3.org/1999/xlink")
   @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
   protected String actuate;

   /**
    * Gets the value of the content property.
    * <p>
    * This accessor method returns a reference to the live list,
    * not a snapshot. Therefore any modification you make to the
    * returned list will be present inside the JAXB object.
    * This is why there is not a <CODE>set</CODE> method for the content property.
    * 
    * <p>
    * For example, to add a new item, do as follows:
    * <pre>
    *    getContent().add(newItem);
    * </pre>
    * 
    * 
    * <p>
    * Objects of the following type(s) are allowed in the list
    * {@link String }
    * {@link JAXBElement }{@code <}{@link String }{@code >}
    * {@link JAXBElement }{@code <}{@link ApplicType }{@code >}
    * {@link JAXBElement }{@code <}{@link String }{@code >}
    * 
    * 
    */
    public List<Serializable> getContent() {
       if (content == null) {
        content = new ArrayList<Serializable>();
       }
       return this.content;
    }
}
    <jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:annox="http://annox.dev.java.net"
    xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">

    <jxb:globalBindings typesafeEnumMemberName="generateName"
        typesafeEnumMaxMembers="1500" collectionType="java.util.ArrayList"
        generateValueClass="true" choiceContentProperty="true">
        <xjc:serializable uid="1" />
    </jxb:globalBindings>

    <jxb:bindings schemaLocation="simpleTypes.xsd" node="/xs:schema">
        <jxb:bindings node="//xs:simpleType[@name='YEAR']">
            <jxb:javaType name="java.lang.Integer" />
        </jxb:bindings>
        <jxb:bindings node="//xs:simpleType[@name='DAY']">
            <jxb:javaType name="java.lang.String" />
        </jxb:bindings>
        <jxb:bindings node="//xs:simpleType[@name='MONTH']">
            <jxb:javaType name="java.lang.String" />
        </jxb:bindings>
        <jxb:bindings node="//xs:simpleType[@name='issnovalueType']">
            <jxb:javaType name="java.lang.String" />
        </jxb:bindings>
    </jxb:bindings>

    <jxb:bindings schemaLocation="attributes.xsd" node="/xs:schema">
        <jxb:bindings node="xs:attribute[@name='inwork']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.lang.String" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
        <jxb:bindings node="//xs:attribute[@name='xrefid']">
            <jxb:property>
                <jxb:baseType>
                    <jxb:javaType name="java.lang.String" />
                </jxb:baseType>
            </jxb:property>
        </jxb:bindings>
    </jxb:bindings>

    <jxb:bindings schemaLocation="complexTypes.xsd" node="/xs:schema">
        <jxb:bindings node="xs:complexType[@name='dmoduleType']">
            <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="dmodule" />
            </annox:annotate>
        </jxb:bindings>
        <jxb:bindings node="//xs:complexType[@name='skillType']">
            <jxb:class name="SkillTypeComplex" />
        </jxb:bindings>
        <!--<jxb:bindings node="//xs:complexType[@name='xrefType']">
              <annox:annotate>
                <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
                    name="xref" />
            </annox:annotate>
            <jxb:bindings node=".//xs:sequence//xs:element[@ref='applic']">
                <jxb:property name="xrefApplic"/>
            </jxb:bindings>
        </jxb:bindings>-->
    </jxb:bindings>

    <jxb:bindings schemaLocation="elementGroups.xsd" node="//xs:group[@name='APPLIC']">
        <jxb:bindings node=".//xs:choice//xs:sequence">
            <jxb:bindings node=".//xs:choice">
                <jxb:property name="assertEvalChoice" />
            </jxb:bindings>
        </jxb:bindings>
    </jxb:bindings>

    <jxb:bindings schemaLocation="dc.xsd" node="/xs:schema">
        <jxb:bindings node="//xs:element[@name='language']">
            <jxb:class name="DcLanguage" />
        </jxb:bindings>
        <jxb:bindings node="//xs:element[@name='subject']">
            <jxb:class name="DcSubject" />
        </jxb:bindings>
        <jxb:bindings node="//xs:element[@name='title']">
            <jxb:class name="DcTitle" />
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

共有1个答案

栾昂雄
2023-03-14

您可以将对象包装在JaxBelement的实例中,以提供根元素信息。如果您从XML模式生成模型,那么在生成的ObjectFactory类中有一些方便的方法。

有关更多信息

  • http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
 类似资料:
  • 根据Camel文档,在这种情况下,可以将JaxbDataFormat设置为'fragement(true) 不幸的是我还是有同样的例外。 是否有一种方法来配置不同的JaxbDataFormat,即定义作为根元素的JAXBElement,就像我在Java中所做的那样 [NA:1.8.0_25]在java.lang.thread.run(thread.java:745)[NA:1.8.0_25]原因:

  • 我在使用继承和JAXB解组时遇到了问题。我已经阅读了大量的示例(特别是http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-xsitype.html上的一个参考博客和一个非常相似的SO问题:JAXB xsi: type subclass unmarshling not work),并且仍然有困难。 像许多其他问题一样,我正在尝试创建

  • 问题内容: 我试图编组多个对象,例如添加到via中。我开始使用此设置: 和 但是,我得到了以下运行时例外: javax.xml.bind.JAXBException:com.jaxb.example.marshall.Book或其任何超类对此上下文都是未知的] 我的类型定义如下。 书:- 图书清单:- 马歇尔代码:- 我正在尝试放置注释(参考:-JAXB异常:该上下文未知的类)。该注释在我的版本中

  • 我对JAXB很陌生,在解封一般对象时遇到了麻烦。问题是我需要能够封送和解封任何对象(java.lang.object)。我成功地进行了封送处理,但是当我运行解封处理时,响应中得到的是一个“ElementNSimpl”对象,而不是我自己的对象。 这是涉及的bean: message.java somebean.java jaxb.index的内容: 生成的xml很好()但是在解封后计算“msg.ge

  • 我正在尝试使用JAXB进行封送处理。 我的输出是这样的: ...但我需要这样的输出: 如果取消对代码的注释,则会得到。没有它,我可以编译,但我不能得到所需的精确输出。 我的豆子长这样: 适配器类

  • 问题内容: 在过去的几天中,我尝试使用MOXy JAXB支持对Hibernate模型的XML编组/解组。尝试执行此操作时,我遇到了hibernate代理对象的问题。考虑类似: 我尝试通过以下方式使用MOXy JAXB映射此代码: 我的问题是,hibernate实例化了通过在User上调用getAddress()获得的地址的代理对象。然后,当JAXB尝试封送对象时,它无法发现它实际上是它要封送的Co