Situation
对于一个项目,我们必须处理相当多的XSD。这些模式中有很多是GML,或者在某种程度上与GML相关。我们使用JAXB2为我们提供Java类,并将XML映射到Java对象。这是令人沮丧的,但对于OGC绑定项目,我们已经取得了很大的进展。
问题
在解组数据时,我们目前似乎遇到了JAXB2中的继承性问题。当我们解组一个集合时,这个集合没有被填充。因此,我使用ValidationEventCollector作为处理程序来检查代码。虽然没有引发Exception,但ValidationEventCollector仍然会给出错误:unexpected element ...
相关代码
我们在推荐绑定和CityGML中使用GML3.1.1,绑定如下: 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"
xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
xmlns:annox="http://annox.dev.java.net" jaxb:extensionBindingPrefixes="xjc inheritance">
<jaxb:globalBindings
fixedAttributeAsConstantProperty="false" typesafeEnumBase="xs:string"
typesafeEnumMemberName="generateName" generateIsSetMethod="true">
<xjc:noValidator />
<xjc:noValidatingUnmarshaller />
</jaxb:globalBindings>
<jaxb:bindings schemaLocation="citygml-2.0/2.0/cityGMLBase.xsd"
node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="net.opengis.citygml.v_2_0" />
</jaxb:schemaBindings>
<jaxb:bindings node="xs:complexType[@name='CityModelType']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement"
name="CityModel"
namespace="http://www.opengis.net/citygml/2.0" />
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>我们也尝试过<xjc:simple />,但这并没有什么不同。除了绑定之外,我们还有一个可以工作的目录(关于其他模式的)和重复的ObjectFactory类在编译前被删除。
以下(部分) XML用于解组
<?xml version="1.0" encoding="UTF-8"?>
<cit:CityModel xmlns:gml="http://www.opengis.net/gml" xmlns:cit="http://www.opengis.net/citygml/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" gml:id="example">
<gml:boundedBy>
<gml:Envelope srsName="http://www.opengis.net/def/crs/EPSG/0/28992">
<gml:lowerCorner>144280.193 414155.258</gml:lowerCorner>
<gml:upperCorner>147300.873 416928.884</gml:upperCorner>
</gml:Envelope>
</gml:boundedBy>
<cit:cityObjectMember>
...
</cit:cityObjectMember>
<cit:cityObjectMember>
...
</cit:cityObjectMember>
<cit:cityObjectMember>
...
</cit:cityObjectMember>
</cit:CityModel>cityObjectMember (问题所在) net.opengis.citygml.v_2_0中ObjectFactory类中的代码
@XmlElementDecl(namespace = "http://www.opengis.net/citygml/2.0", name = "cityObjectMember", substitutionHeadNamespace = "http://www.opengis.net/gml", substitutionHeadName = "featureMember")
public JAXBElement<FeaturePropertyType> createCityObjectMember(FeaturePropertyType value) {
return new JAXBElement<FeaturePropertyType>(_CityObjectMember_QNAME, FeaturePropertyType.class, null, value);
}使用相应的解编组代码。
Unmarshaller um = JAXBContext.newInstance("net.opengis.citygml.v_2_0:net.opengis.gml").createUnmarshaller();
JAXB2ValidationEventCollector vec = new JAXB2ValidationEventCollector();
um.setEventHandler(vec);
Object unmarshalled = um.unmarshal(this.getFile());
// Check for errors, when there are (validation) errors, throw them to System.err.
if (vec.hasEvents()) {
for (ValidationEvent ve : vec.getEvents()) {
System.err.println(String.format("[Line: %d Column: %d] %s", ve.getLocator().getLineNumber(),
ve.getLocator().getColumnNumber(), ve.getMessage()));
}
}作为ValidationEventCollector:
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.util.ValidationEventCollector;
class JAXB2ValidationEventCollector extends ValidationEventCollector {
@Override
public boolean handleEvent(ValidationEvent event) {
super.handleEvent(event);
return true;
}
}The issues!
首先,正如我已经提到的,cityObjectMember没有被解析。这是问题的根源。这就是为什么我将eventHandler添加到解组程序中。这导致了以下错误:
[Line: 56 Column: 27] unexpected element (uri:"http://www.opengis.net/citygml/2.0", local:"cityObjectMember"). Expected elements are <{http://www.opengis.net/gml}ellipsoidName>,<{http://www.opengis.net/gml}meridianName>,<{http://www.opengis.net/gml}featureMember>,<{http://www.opengis.net/citygml/2.0}_GenericApplicationPropertyOfCityModel>,<{http://www.opengis.net/gml}parameterName>,<{http://www.opengis.net/gml}groupName>,<{http://www.opengis.net/gml}srsName>,<{http://www.opengis.net/gml}metaDataProperty>,<{http://www.opengis.net/gml}priorityLocation>,<{http://www.opengis.net/gml}location>,<{http://www.opengis.net/gml}coordinateOperationName>,<{http://www.opengis.net/gml}datumName>,<{http://www.opengis.net/gml}featureMembers>,<{http://www.opengis.net/gml}methodName>,<{http://www.opengis.net/gml}boundedBy>,<{http://www.opengis.net/gml}csName>,<{http://www.opengis.net/gml}description>,<{http://www.opengis.net/gml}name>说实话..。我现在被困住了。我不想再继续下去了。因此,如果有人知道问题的答案或根源,那就太好了。谢谢:)。
发布于 2014-03-20 10:50:32
我是"OGC模式和工具项目“的作者。您的绑定似乎是正确的,对象工厂看起来很好,它应该可以工作。您正在经历的核心问题是,由于某些原因,gml:功能成员不会被cit:cityObjectMember所取代。很难说为什么。我试图通过分析JAXB为类创建的运行时模型来解决这个问题。不知何故,您的@XmlElementDecl on createCityObjectMember被忽略了。
对于这类事情,我的方法通常是采取一些不起作用的东西,然后尝试缩小差距,从而找出本质上是一个问题的尖端。
在本例中,我将使用代码并构建一个替换工作的最小示例。然后我试着减少你不工作的例子,剥夺它不相关的属性等等。最后,你会得到一个边缘,这将给你一个线索,什么是错的。
我猜,如果不深入的话,那就是范围了。也许gml:featureMemeber的替代是有限的,而且您的替换与“极限”不匹配。很难说没有调试。
请随时与我联系( gmx上的valikov),提供您的映射(或整个项目),我将尽力帮助您。
https://stackoverflow.com/questions/22528951
复制相似问题