开发者

Unexpected JAXB error

开发者 https://www.devze.com 2022-12-25 13:51 出处:网络
From the documentation it\'s clear I need to use the following to get a simple unmarshalling to occur from my XML file/schema:

From the documentation it's clear I need to use the following to get a simple unmarshalling to occur from my XML file/schema:

JAXBContext jc = JAXBContext.newInstance("PackageName");

where PackageName is my package name. I've looked on google for a bit to no avail, to find out why I'm then getting this runtime error:

Line:Col[2:142]:cvc-elt.1: Cannot find the declaration of element 'myconfig'.
Line:Col[2:142]:unexpected element (uri:"http://www.w3.org", local:"myconfig"). Expected elements are <{}myconfig>
Caught UnmarshalException

EDIT: Note: the plugin forced me to specify a package name. I gave it the same package as all my application class files are in as I wanted all the JAXB classes and my app together. If my package is PackageName, it then created all the JAXB class files in PackageName.PackageName. I then had to move them out of PackageName.PackageName (which it automatically created) back into the tree where my app is (PackageName). This doesn't feel right to me. EDIT END

The following occurs at the top of all my class files, including the ones the XJC JAXB plugin for eclipse created for me:

package PackageName;

so why is this error occurring?

EDIT: The schema is quite big so I don't want to paste it all in here, and the ObjectFactory.java file is consequently quite large. It begins

package PackageName;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

and ends

/**
 * Create an instance of {@link JAXBElement }{@code <}{@link MyConfigType }{@code >}}
 * 
 */
@XmlElementDecl(namespace = "", name = "myconfig")
public JAXBElement<MyConfigType> createMyconfig(MyConfigType value) {
    return new JAXBElement<MyConfigType>(_Myconfig_QNAME, MyConfigType.class, null, value);
}

So all quite standard. The top of the schema (I'm an XML schema newb and this was supplied to me), is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">

which also looks straight forward. The schema for the root element is this:

<xs:complexType name="MyConfigType">
    <xs:sequence>
        <xs:element name="tips" type="TipType" minOccurs="0"
            maxOccurs="unbounded">
            <xs:key name="unique_abc_id">
                <xs:selector xpath="./abc" />
                <xs:field xpath="@id" />
            </xs:key>
            <xs:key name="unique_def_id">
                <xs:selector xpath="./def" />
                <xs:field xpath="@id" />
            </xs:key>
        </xs:element>
    </xs:sequence>
    <xs:attribute ref="noNamespaceschemaLocation" />
</xs:complexType>

The s开发者_StackOverflowupplied config which I'm asking it to validate at the same time as unmarshal starts:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<myconfig xmlns="http://www.w3.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceschemaLocation="myconfig.xsd">
    <firsttag...>

I'm stumped.

EDIT END


For this to work:

JAXBContext jc = JAXBContext.newInstance("PackageName");

You need to have one or both of these:

  • an ObjectFactory.class in that directory
  • a jaxb.index file. This file should contain a list of class names in that package.

Either of those would describe the classes that should be available to JAXB.

0

精彩评论

暂无评论...
验证码 换一张
取 消