gpt4 book ai didi

java - 无法创建具有特定类的 JAXB 实例

转载 作者:搜寻专家 更新时间:2023-11-01 03:24:12 25 4
gpt4 key购买 nike

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

最佳答案

您需要将 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);
}

}

input.xml/输出

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

关于java - 无法创建具有特定类的 JAXB 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19515790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com