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

当使用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()的帖子。但是仍然会发生错误,我无法真正从stacktrace中提取任何有用的信息。

编组代码:
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上下文调用仍然存在问题,我通过在用冒号分隔的一个String中引用所有包来解决该问题。

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编码(marshal)XBRL实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28039905/

10-08 21:54