4

我想编组一个 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
....
}
4

2 回答 2

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 {

}

我的对象工厂

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

}

输入.xml/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<foo xmlns="http://www.xbrl.org/2003/linkbase">
    <footnoteLink/>
</foo>
于 2013-10-22T11:03:54.177 回答
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)

于 2013-10-22T11:05:56.203 回答