根据下面的回复,用我的最新示例更新这篇文章。
我正在尝试使用 MOXY 创建一个 DynamicJAXBContext 使用一个简单的模式,该模式使用 GML 2.1.2 模式(导入 xlink)。似乎 moxy 在导入 GML 模式(或 GML 导入的 xlink 模式)时遇到问题。我尝试向 DynamicJAXBContextFactory 提供 jaxb 绑定文件,但这不起作用。
我正在使用 eclipselink 2.4.2(每晚 2/20)和 jaxb-xjc 2.1.12。正在生成的异常如下:
异常 [EclipseLink-0](Eclipse 持久性服务 - 2.4.2.v20130220-bf58d47):org.eclipse.persistence.exceptions.IntegrityException 描述符
例外:
异常 [EclipseLink-217] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47):org.eclipse.persistence.exceptions.DescriptorException 异常描述:XMLDirectMapping/XMLCompositeDirectCollectionMapping 的 XPath 无效。XPath 必须包含属性的@ 符号或以/text() 结尾的文本节点。例如:“@name”或“name/text()” 映射:org.eclipse.persistence.oxm.mappings.XMLDirectMapping[title-->ns2:title] 描述符:XMLDescriptor(org.w3._1999.xlink.Extended --> [])
异常 [EclipseLink-217] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47):org.eclipse.persistence.exceptions.DescriptorException 异常描述:XMLDirectMapping/XMLCompositeDirectCollectionMapping 的 XPath 无效。XPath 必须包含属性的@ 符号或以/text() 结尾的文本节点。例如:“@name”或“name/text()” 映射:org.eclipse.persistence.oxm.mappings.XMLDirectMapping[xlinkTitle1-->ns2:title] 描述符:XMLDescriptor(org.w3._1999.xlink.LocatorType --> [DatabaseTable(ns2:locator)])
异常 [EclipseLink-217] (Eclipse Persistence Services - 2.4.2.v20130220-bf58d47):org.eclipse.persistence.exceptions.DescriptorException 异常描述:XMLDirectMapping/XMLCompositeDirectCollectionMapping 的 XPath 无效。XPath 必须包含属性的@ 符号或以/text() 结尾的文本节点。例如:“@name”或“name/text()” 映射:org.eclipse.persistence.oxm.mappings.XMLDirectMapping[xlinkTitle2-->ns2:title] 描述符:XMLDescriptor(org.w3._1999.xlink.ArcType --> [数据库表(ns2:arc)])
运行时异常:
在 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:638) 在 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:574) 在 org.eclipse.persistence.internal .sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:533) 在 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:777) 在 org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl .java:735) 在 org.eclipse.persistence.oxm.XMLContext$XMLContextState.(XMLContext.java:940) 在 org.eclipse.persistence.oxm.XMLContext$XMLContextState.(XMLContext.java:915) 在 org.eclipse。 org.eclipse 上的 persistence.oxm.XMLContext.(XMLContext.java:194)。persistence.jaxb.dynamic.DynamicJAXBContext$SchemaContextInput.createContextState(DynamicJAXBContext.java:332) 在 org.eclipse.persistence.jaxb.JAXBContext.(JAXBContext.java:169) 在 org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext.( DynamicJAXBContext.java:70) 在 org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory.createContextFromXSD(DynamicJAXBContextFactory.java:309) 在 ogc.catalog.moxy.TestMoxy.testMoxy(TestMoxy.java:45)java:309) 在 ogc.catalog.moxy.TestMoxy.testMoxy(TestMoxy.java:45)java:309) 在 ogc.catalog.moxy.TestMoxy.testMoxy(TestMoxy.java:45)
这是我的测试moxy类:
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory;
import org.junit.Test;
public class TestMoxy {
private static final String SCHEMA = "example-feature.xsd";
@Test
public void testMoxy() throws JAXBException {
System.setProperty("javax.xml.bind.context.factory",
"org.eclipse.persistence.jaxb.DynamicJAXBContextFactory");
System.setProperty("com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.noCorrectnessCheck",
"true");
InputStream schema = this.getClass().getClassLoader().getResourceAsStream(SCHEMA);
Source schemaSource = new StreamSource(schema);
schemaSource.setSystemId(SCHEMA);
InputStream xjbStream = this.getClass().getClassLoader()
.getResourceAsStream("xlink-bindings.xjb");
Source xjbSource = new StreamSource(xjbStream);
xjbSource.setSystemId(SCHEMA);
Map<String, Object> props = new HashMap<String, Object>();
props.put(DynamicJAXBContextFactory.EXTERNAL_BINDINGS_KEY, xjbSource);
try {
DynamicJAXBContext jaxbContext = DynamicJAXBContextFactory.createContextFromXSD(
schemaSource, null, null, props);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这是example-feature.xsd:
<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema elementFormDefault='qualified' targetNamespace='http://example.org'
xmlns:example='http://example.org'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:gml='http://www.opengis.net/gml'>
<xsd:import namespace='http://www.opengis.net/gml' schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"/>
<xsd:complexType name='example_featureType'>
<xsd:complexContent>
<xsd:extension base='gml:AbstractFeatureType'>
<xsd:sequence>
<xsd:element maxOccurs='1' minOccurs='1' name='id' nillable='false' type='xsd:long'/>
<xsd:element maxOccurs='1' minOccurs='1' name='version' nillable='false' type='xsd:long'/>
<xsd:element maxOccurs='1' minOccurs='0' name='access_date' nillable='true' type='xsd:dateTime'/>
<xsd:element maxOccurs='1' minOccurs='0' name='ground_geom' nillable='true' type='gml:PolygonPropertyType'/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<xsd:element name='exampleFeature' substitutionGroup='gml:_Feature' type='example:example_featureType'/>
</xsd:schema>
和 xlink-bindings.xjb:
<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionBindingPrefixes="xjc">
<jaxb:bindings schemaLocation="http://www.w3.org/1999/xlink.xsd">
<jaxb:bindings node="//xs:attributeGroup[@name='locatorAttrs']//xs:attribute[@ref='xlink:title']">
<jaxb:property name="xlink:title1" />
</jaxb:bindings>
<jaxb:bindings node="//xs:attributeGroup[@name='arcAttrs']//xs:attribute[@ref='xlink:title']">
<jaxb:property name="xlink:title2" />
</jaxb:bindings>
</jaxb:bindings>
<jaxb:bindings schemaLocation="http://schemas.opengis.net/gml/2.1.2/feature.xsd"
node="/xs:schema">
<jaxb:globalBindings fixedAttributeAsConstantProperty="false"
typesafeEnumBase="xs:string" typesafeEnumMemberName="generateName" generateIsSetMethod="true"
generateMixedExtensions="true">
<xjc:noValidator />
<xjc:noValidatingUnmarshaller />
</jaxb:globalBindings>
<jaxb:schemaBindings>
<jaxb:package name="ogc.schema.opengis.gml.v_2_1_2" />
<jaxb:nameXmlTransform>
<jaxb:elementName suffix="Element" />
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
<jaxb:bindings node="xs:element[@name='_geometryProperty']">
<jaxb:class name="AbstractGeometryProperty" />
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
注意xjc 对我有用: xjc -extension example-feature.xsd -b xlink-bindings.xjb
xjc 版本“JDK 6 中的 JAXB 2.1.10” *java 版本“1.6.0_38”*