无法使用特定类创建JAXB实例

时间:2013-10-22 10:50:47

标签: java jaxb

我想编组一个java对象,但是javax.xml.bind.JAXBContext会抛出异常。

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

我得到了;

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.

编辑:以下是给出错误的代码。我认为这段代码一定有问题。

@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
....
}

2 个答案:

答案 0 :(得分:2)

您需要在用于创建ObjectFactory的类列表中包含JAXBContext类。或者,您可以在包和生成的模型上创建JAXBContext,它会自动找到ObjectFactory

Java模型

<强>富

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;

}

<强>酒吧

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);
    }

}

<强>包信息

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

import javax.xml.bind.annotation.*;

演示代码

<强>演示

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中/输出

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

答案 1 :(得分:0)

请查看以下代码以供参考

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);

}
}

以下行显示您要编组的对象的类。 JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class)