JAXB DOM节点解组省略了元素

时间:2012-12-19 22:43:03

标签: java dom jaxb ogc

我正在从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 Element,并确认Filter在DOM树中。

要获取我正在使用的JAXB对象,我从jvnet OGC project检出了SVN存储库,我确实需要编译WFS 2.0模式,但Filter和GML模式已经完成。

非常感谢任何见解。

1 个答案:

答案 0 :(得分:1)

示例XML来自第81页的OGC WFS 2.0规范文档(09-025r1)。有一个拼写错误的过滤器的xmlns为&#34; http://www.opengis.org/fes/ 2.0&#34;什么时候应该是&#34; http://www.opengis.net/fes/2.0"。 JAXB认为这是一个无法识别的元素并将其丢弃在地板上。