hellzone
hellzone

Reputation: 5236

Can not create JAXB instance with specific class

I want to marshall a java object but javax.xml.bind.JAXBContext throws exception.

List<Class> list = new ArrayList<Class>();
list.add(obj.getClass());
list.add(ObjectFactory.getClass());
JAXBContext.newInstance(list);  //this line throws exception

I got;

ex = (com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException) com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
There's no ObjectFactory with an @XmlElementDecl for the element {http://www.xbrl.org/2003/linkbase}footnoteLink.

EDIT: Here is the code which gives error. I think there must be something wrong with this code.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"schemaRef",
"linkbaseRef",
"roleRef",
"arcroleRef",
"itemOrTupleOrContext"
})
@XmlRootElement(name = "xbrl")
public class Xbrl {

@XmlElement(namespace = "http://www.xbrl.org/2003/linkbase", required = true)
protected List<SimpleType> schemaRef;
@XmlElement(namespace = "http://www.xbrl.org/2003/linkbase")
protected List<LinkbaseRef> linkbaseRef;
@XmlElement(namespace = "http://www.xbrl.org/2003/linkbase")
protected List<RoleRef> roleRef;
@XmlElement(namespace = "http://www.xbrl.org/2003/linkbase")
protected List<ArcroleRef> arcroleRef;
@XmlElementRefs({
    @XmlElementRef(name = "tuple", namespace = "http://www.xbrl.org/2003/instance", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "footnoteLink", namespace = "http://www.xbrl.org/2003/linkbase", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "item", namespace = "http://www.xbrl.org/2003/instance", type = JAXBElement.class, required = false),
    @XmlElementRef(name = "unit", namespace = "http://www.xbrl.org/2003/instance", type = Unit.class, required = false),
    @XmlElementRef(name = "context", namespace = "http://www.xbrl.org/2003/instance", type = Context.class, required = false)
})
protected List<Object> itemOrTupleOrContext; //this line
....
}

Upvotes: 4

Views: 8901

Answers (2)

bdoughan
bdoughan

Reputation: 148977

You need to include the ObjectFactory class in the list of classes used to create the JAXBContext. Alternatively you could create the JAXBContext on the package and of the generated model which would find the ObjectFactory automatically.

Java Model

Foo

package forum19515790;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlElementRef(name="footnoteLink", namespace="http://www.xbrl.org/2003/linkbase")
    private JAXBElement<Bar> footnoteLink;

}

Bar

package forum19515790;

public class Bar {

}

MyObjectFactory

package forum19515790;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class MyObjectFactory {

    @XmlElementDecl(name="footnoteLink", namespace="http://www.xbrl.org/2003/linkbase")
    public JAXBElement<Bar> createBar(Bar bar) {
        return new JAXBElement<Bar>(new QName("http://www.xbrl.org/2003/linkbase", "footnoteLink"), Bar.class, bar);
    }

}

package-info

@XmlSchema(
    namespace="http://www.xbrl.org/2003/linkbase",
    elementFormDefault=XmlNsForm.QUALIFIED)
package forum19515790;

import javax.xml.bind.annotation.*;

Demo Code

Demo

package forum19515790;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class, MyObjectFactory.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum19515790/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns="http://www.xbrl.org/2003/linkbase">
    <footnoteLink/>
</foo>

Upvotes: 2

shikjohari
shikjohari

Reputation: 2288

Please do have a look to the following code for reference

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
class Employee{

    private int empId;

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

}

public class TEst {
public static void main(String[] args) throws JAXBException {

    Employee emp = new Employee();
    JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    jaxbMarshaller.marshal(emp, System.out);

}
}

The below line takes the class of which object you want to marshal. JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class)

Upvotes: 0

Related Questions