tags:

views:

185

answers:

1

Hello,

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 supplied 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

+1  A: 

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.

Chris Dail
Ah. Sorry forgot to mention, I already had xjc create the objectfactory.java file -it's in with the schema reference files. Where do I go next?
Mark Lewis
@Mark Lewis I would need some more information about the classes itself. It looks a schema validation issue but without seeing the Class I don't know if I can tell you much.
Chris Dail
@Chris Dail, thanks for the answer, I've edited some problem code into my question to flesh it out a bit. Maybe that will illustrate my issue in more depth. Thanks IA
Mark Lewis