gpt4 book ai didi

java - 在 IDREF 属性中找到获取对象 ""的 JAXB,但此对象没有 ID

转载 作者:数据小太阳 更新时间:2023-10-29 02:18:36 24 4
gpt4 key购买 nike

这是一个使用 SCORM 文件的测试用例,用于 imsmanifest,xml。这个 XML 已经使用了大约 5 年或更长时间,并且作为一个标准我不想更改它,除非需要让它工作。

您可以找到 xsd 文件 here

错误发生在<organizations default="CYBER4.ORG">之间和 <organization identifier="CYBER4.ORG">

在我的项目中,这是来 self 的 pom.xml 的条目对于我的 jaxb 版本

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.11</version>
</dependency>

生成我运行的 Java 代码(这是为 Ubuntu 14.04 安装 xjc)

$ xjc -version
xjc 2.2.4-2
$ xjc -verbose -p org.cyber4.scorm2004.xml.manifest.imscp imscp_v1p2.xsd

输出生成(除其他外)

public class OrganizationsType {

@XmlAttribute(name = "default")
@XmlIDREF
@XmlSchemaType(name = "IDREF")
protected Object _default;

}

public class OrganizationType {

@XmlAttribute(name = "identifier", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlID
@XmlSchemaType(name = "ID")
protected String identifier;

}

这是测试代码

package org.cyber4.scorm2004.build;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import org.cyber4.scorm2004.xml.manifest.imscp.ManifestMetadataType;
import org.cyber4.scorm2004.xml.manifest.imscp.ManifestType;
import org.cyber4.scorm2004.xml.manifest.imscp.ObjectFactory;
import org.cyber4.scorm2004.xml.manifest.imscp.OrganizationType;
import org.cyber4.scorm2004.xml.manifest.imscp.OrganizationsType;
import org.cyber4.scorm2004.xml.manifest.imscp.ResourcesType;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

public class TestSCORMBuilder {

public static void main(String args[]) {

try {

ObjectFactory objectFactory = new ObjectFactory();

/*
* <metadata />
*/
ManifestMetadataType metadataType = objectFactory.createManifestMetadataType();

/*
* <organizations default="CYBER4.ORG">
* <organization identifier="CYBER4.ORG" />
* </organizations>
*/

// https://java.net/jira/browse/JAXB-872
OrganizationsType organizationsType = objectFactory.createOrganizationsType();
organizationsType.setDefault("CYBER4.ORG");

OrganizationType organizationType = objectFactory.createOrganizationType();
organizationType.setIdentifier("CYBER4.ORG");

organizationsType.getOrganization().add(organizationType);

/*
* <resources />
*/
ResourcesType resourcesType = objectFactory.createResourcesType();

/*
* <manifest>
* <metadata/ >
* <organizations default="CYBER4.ORG">
* <organization identifier="CYBER4.ORG" />
* </organizations>
* <resources />
* <manifest>
*/

ManifestType manifestType = objectFactory.createManifestType();

manifestType.setMetadata(metadataType);
manifestType.setOrganizations(organizationsType);
manifestType.setResources(resourcesType);

JAXBContext context = JAXBContext.newInstance("org.cyber4.scorm2004.xml.manifest.imscp");

Marshaller marshaller = context.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
"http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd");

marshaller.marshal(objectFactory.createManifest(manifestType), System.out);

} catch (Exception exception) {
exception.printStackTrace();
}
}

}

当我运行代码时出现这个错误

javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: Object "CYBER4.ORG" is found in an IDREF property but this object doesnt have an ID.]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:95)
at org.cyber4.scorm2004.build.TestSCORMBuilder.main(TestSCORMBuilder.java:73)
Caused by: com.sun.istack.internal.SAXException2: Object "CYBER4.ORG" is found in an IDREF property but this object doesnt have an ID.
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.errorMissingId(XMLSerializer.java:1045)
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor$IDREFTransducedAccessorImpl.print(TransducedAccessor.java:275)
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor$IDREFTransducedAccessorImpl.print(TransducedAccessor.java:254)
at com.sun.xml.internal.bind.v2.runtime.property.AttributeProperty.serializeAttributes(AttributeProperty.java:86)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeAttributes(ClassBeanInfoImpl.java:360)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:678)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:343)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:685)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:143)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl$1.serializeBody(ElementBeanInfoImpl.java:115)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeBody(ElementBeanInfoImpl.java:317)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:324)
at com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot(ElementBeanInfoImpl.java:60)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
... 3 more

如果我注释掉第37行

organizationsType.setDefault("CYBER4.ORG");

它生成这个 XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<manifest xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd">
<metadata/>
<organizations>
<organization identifier="CYBER4.ORG"/>
</organizations>
<resources/>
</manifest>

但它缺少

 default="CYBER4.ORG"

<organizations>这是 imsmanifest.xml 所必需的有效。

这看起来像 this bug但我想确定我没有遗漏任何东西。

最佳答案

default 不应包含要引用的对象的 id,而是与要引用的对象具有相同 id 的对象。

尝试替换:

organizationsType.setDefault("CYBER4.ORG");    

与:

OrganizationType o = new OrganizationType()   
o.setIdentifier("CYBER4.ORG");
organizationsType.setDefault(o);

如果 organizationType 已经被设置,你也可以尝试:

organizationsType.setDefault(organizationType);    

关于java - 在 IDREF 属性中找到获取对象 ""的 JAXB,但此对象没有 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25224987/

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