我正在从OGC解组一些 XML并遇到一个问题,即并非所有元素都进入最终对象。
这是示例 XML
<?xml version="1.0"?>
<wfs:CreateStoredQuery xmlns:wfs="http://www.opengis.net/wfs/2.0"
xmlns:fes="http://www.opengis.org/fes/2.0" xmlns:gml="http://www.opengis.net/gml/3.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:myns="http://www.someserver.com/myns"
xsi:schemaLocation="http://www.opengis.net/wfs/2.0
http://schemas.opengis.net/wfs/2.0/wfs.xsd"
service="WFS" version="2.0.0">
<wfs:StoredQueryDefinition id="urn:StoredQueries:FeaturesInPolygon">
<wfs:Title>Features In Polygon</wfs:Title>
<wfs:Abstract>Find all the features in a Polygon.</wfs:Abstract>
<wfs:Parameter name="AreaOfInterest" type="gml:PolygonType" />
<wfs:QueryExpressionText returnFeatureTypes="myns:Parks"
language="urn:ogc:def:queryLanguage:OGC-WFS::WFS_QueryExpression"
isPrivate="false">
<wfs:Query typeNames="myns:Parks">
<fes:Filter>
<fes:Within>
<fes:ValueReference>geometry</fes:ValueReference>
<gml:Polygon>
<gml:exterior>
<gml:LinearRing>
<gml:posList>0 0 100 0 100 100 0 100 0 0</gml:posList>
</gml:LinearRing>
</gml:exterior>
</gml:Polygon>
</fes:Within>
</fes:Filter>
</wfs:Query>
</wfs:QueryExpressionText>
</wfs:StoredQueryDefinition>
</wfs:CreateStoredQuery>
这是违规区域的 XSD
<xsd:complexType name="StoredQueryDescriptionType">
<xsd:sequence>
<xsd:element ref="wfs:Title" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="wfs:Abstract" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="ows:Metadata" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="Parameter"
type="wfs:ParameterExpressionType"
minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="QueryExpressionText"
type="wfs:QueryExpressionTextType"
minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:anyURI" use="required"/>
</xsd:complexType>
<xsd:complexType name="ParameterExpressionType">
<xsd:sequence>
<xsd:element ref="wfs:Title" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="wfs:Abstract" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="ows:Metadata" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="type" type="xsd:QName" use="required"/>
</xsd:complexType>
<xsd:complexType name="QueryExpressionTextType" mixed="true">
<xsd:choice>
<xsd:any namespace="##other" processContents="skip"
minOccurs="0" maxOccurs="unbounded"/>
<xsd:any namespace="##targetNamespace" processContents="skip"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
<xsd:attribute name="returnFeatureTypes"
type="wfs:ReturnFeatureTypesListType" use="required"/>
<xsd:attribute name="language" type="xsd:anyURI" use="required"/>
<xsd:attribute name="isPrivate" type="xsd:boolean" default="false"/>
</xsd:complexType>
<xsd:simpleType name="ReturnFeatureTypesListType">
<xsd:list itemType="xsd:QName"/>
</xsd:simpleType>
搞砸的部分是 QueryExpressionTextType 下的 xsd:any。它具有属性 processContents="skip" 导致 JAXB 创建 DOM 对象而不是 JAXB 类。我想我以后只需使用 JAXB 上下文来解组 DOM 节点,这是我尝试过的一些示例代码。
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("test/res/createQuery.xml");
JAXBContext context = JAXBContext.newInstance(
net.opengis.gml.v_3_2_1.ObjectFactory.class,
net.opengis.wfs.v_2_0_0.ObjectFactory.class,
net.opengis.filter.v_2_0_0.ObjectFactory.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<?> jElem = (JAXBElement<?>) unmarshaller.unmarshal(in);
CreateStoredQueryType create = (CreateStoredQueryType) jElem.getValue();
List<StoredQueryDescriptionType> descriptions = create
.getStoredQueryDefinition();
StoredQueryDescriptionType desc = descriptions.get(0);
List<QueryExpressionTextType> texts = desc.getQueryExpressionText();
QueryExpressionTextType text = texts.get(0);
List<Object> contents = text.getContent();
Node node = (Node) contents.get(0);
jElem = (JAXBElement<?>) unmarshaller.unmarshal(node);
QueryType query = (QueryType) jElem.getValue();
// prints[{http://www.someserver.com/myns}Parks]
System.out.println(query.getTypeNames());
// is false
System.out.println(query.isSetAbstractSelectionClause());
}
它可以很好地获取 Query 对象(包括 typeNames 属性)。但它会跳过 Filter 对象。Filter 对象继承自 AbstractSelectionClause,因此应该在返回的对象中设置它,但事实并非如此。我调试并检查了 DOM 元素并确认过滤器在 DOM 树中。
为了获得我正在使用的 JAXB 对象,我从jvnet OGC 项目中检查了 SVN 存储库,我确实必须编译 WFS 2.0 模式,但过滤器和 GML 模式已经完成。
任何见解将不胜感激。