gpt4 book ai didi

java - 无法使用 JAXB 编码 XBRL 实例

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:33 25 4
gpt4 key购买 nike

我正在尝试使用 JAXB 创建 XBRL 实例。我可以使用基于 xbrl-instance 模式的 xjc 成功创建 java 模型 ( http://www.xbrl.org/2003/xbrl-instance-2003-12-31.xsd )

当使用 xjc 创建 java 类时,我遵循了这篇文章的建议(除了 maven 部分,因为我没有使用它):JAXB fails to generate Java classes for XBRL

xbrl_bindings.xjb:

<bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" version="2.1">
<bindings schemaLocation="xl-2003-12-31.xsd" version="1.0">
<bindings node="//xs:schema//xs:element[@name='title']">
<property name="xlTitle"/>
</bindings>
</bindings>
</bindings>

所以现在我正在尝试生成一个 XBRL 实例,但是在初始化上下文时我得到了一个 Java.lang.IllegalArgumentException。我发现另一篇文章建议通过引用包和 ObjectFactory(使用类加载器)来调用 newInstance()。但错误仍然发生,我无法真正从堆栈跟踪中提取任何有用的信息。

编码代码:

public class JaxbXbrlInstanceCreationTest {

public static void main(String[] args) {

org.xbrl._2003.instance.ObjectFactory instObjFact = new ObjectFactory();
Xbrl xbrl = instObjFact.createXbrl();
xbrl.setId("Test");

org.xbrl._2003.linkbase.ObjectFactory linkbaseObjFact = new org.xbrl._2003.linkbase.ObjectFactory();
org.xbrl._2003.xlink.ObjectFactory xlinkObjFact = new org.xbrl._2003.xlink.ObjectFactory();
org.xbrl._2003.xlink.SimpleType schemaRefvalue = xlinkObjFact.createSimpleType();

schemaRefvalue.setHref("http://eiopa.europa.eu/eu/xbrl/s2md/fws/solvency/solvency2/2014-07-23/mod/ars.xsd");
schemaRefvalue.setType("simple");
List<SimpleType> schemaRefList = xbrl.getSchemaRef();
schemaRefList.add(linkbaseObjFact.createSchemaRef(schemaRefvalue).getValue());

List<Object> contextList = xbrl.getItemOrTupleOrContext();

Context context = instObjFact.createContext();
context.setId("Context");

ContextEntityType entityType = instObjFact.createContextEntityType();
Identifier identifier = new Identifier();
identifier.setValue("someone");
identifier.setScheme("http://www.example.com");
entityType.setIdentifier(identifier);
context.setEntity(entityType);

ContextPeriodType contextPeriodType = instObjFact.createContextPeriodType();
contextPeriodType.setInstant("2014-02-28");
context.setPeriod(contextPeriodType);

ContextScenarioType contextScenarioType = instObjFact.createContextScenarioType();
context.setScenario(contextScenarioType);

contextList.add(context);

try {
//JAXBContext jaxbContext = JAXBContext.newInstance(Xbrl.class, org.xbrl._2003.instance.ObjectFactory.class);
JAXBContext jaxbContext = JAXBContext.newInstance("org.xbrl._2003.instance", org.xbrl._2003.instance.ObjectFactory.class.getClassLoader());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
marshaller.marshal( xbrl, new FileOutputStream( "C:/Users/peter.goldenbogen/Desktop/test.xbrl" ) );

} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

堆栈跟踪:

Exception in thread "main" java.lang.IllegalArgumentException: can't parse argument number: ''{0}''
at java.text.MessageFormat.makeFormat(Unknown Source)
at java.text.MessageFormat.applyPattern(Unknown Source)
at java.text.MessageFormat.<init>(Unknown Source)
at java.text.MessageFormat.format(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.Messages.format(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.ReferencePropertyInfoImpl.calcTypes(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.ReferencePropertyInfoImpl.link(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.ClassInfoImpl.link(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.RuntimeClassInfoImpl.link(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.ModelBuilder.link(Unknown Source)
at com.sun.xml.internal.bind.v2.model.impl.RuntimeModelBuilder.link(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
at javax.xml.bind.ContextFinder.find(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
at de.sample.test.JaxbXbrlInstanceCreationTest.main(JaxbXbrlInstanceCreationTest.java:76)
Caused by: java.lang.NumberFormatException: For input string: "''{0}''"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
... 27 more

最佳答案

我可以通过发现实际堆栈跟踪由于一个已知错误而产生误导来解决我的问题,该错误在翻译 jaxb 错误消息时发生。看这里:https://java.net/jira/browse/JAXB-1017

毕竟我的 jaxb 上下文调用仍然存在问题,我通过在一个由冒号分隔的字符串中引用所有包来解决这个问题。

JAXBContext jaxbContext = JAXBContext.newInstance("org.xbrl._2003.instance:org.xbrl._2003.linkbase:org.xbrl._2003.xlink");

编辑:

更改语言环境会导致信息更丰富的错误消息:

Locale.setDefault(Locale.ENGLISH);` 

输出:

com.sun.xml.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.
this problem is related to the following location:
at protected java.util.List org.xbrl._2003.instance.Xbrl.itemOrTupleOrContext
at org.xbrl._2003.instance.Xbrl

关于java - 无法使用 JAXB 编码 XBRL 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28039905/

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