1

XML、実際には Geography Markup Language (GML) を非整列化しようとしています。

http://play.golang.org/p/qS6GjCOtHFに例があります

最初の 2 つの問題:

タグ "boundedBy>Envelope>lowerCorner" を持つ xml の main.FeatureCollection フィールド "LowerCorner" の読み取りエラーが、タグ "boundedBy>Envelope" を持つフィールド "Envelope" と競合します

それを修正する方法がわかりません。私はそれらをコメントアウトし、エラーなしで GML をアンマーシャリングFeaturesしましたが、FeatureCollection.

手がかりはありますか?

GML の例は次のとおりです。

<?xml version="1.0" encoding="UTF-8"?>
<gml:FeatureCollection xmlns:gml="http://www.opengis.net/gml"
    xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:fme="http://www.safe.com/gml/fme" xsi:schemaLocation="http://www.safe.com/gml/fme tblMainGML.xsd">
    <gml:boundedBy>
        <gml:Envelope srsName="EPSG:3112" srsDimension="2">
            <gml:lowerCorner>45.2921142578125 -80.2166748046875</gml:lowerCorner>
            <gml:upperCorner>169.000122070313 -9.14251708984375</gml:upperCorner>
        </gml:Envelope>
    </gml:boundedBy>
    <gml:featureMember>
        <fme:GML gml:id="id5255fa48-42b3-43d1-9e0d-b2ba8b57a936">
            <fme:OBJECTID>1</fme:OBJECTID>
            <fme:RECORD_ID>QLD48234</fme:RECORD_ID>
            <fme:NAME>HATCHMAN POINT</fme:NAME>
            <fme:FEAT_CODE>PT</fme:FEAT_CODE>
            <fme:CGDN>N</fme:CGDN>
            <fme:AUTHORITY_ID>QLD</fme:AUTHORITY_ID>
            <fme:CONCISE_GAZ>N</fme:CONCISE_GAZ>
            <fme:LATITUDE>-12.58361</fme:LATITUDE>
            <fme:lat_degrees>-12</fme:lat_degrees>
            <fme:lat_minutes>35</fme:lat_minutes>
            <fme:lat_seconds>0</fme:lat_seconds>
            <fme:LONGITUDE>141.62583</fme:LONGITUDE>
            <fme:long_degrees>141</fme:long_degrees>
            <fme:long_minutes>37</fme:long_minutes>
            <fme:long_seconds>32</fme:long_seconds>
            <fme:STATE_ID>QLD</fme:STATE_ID>
            <fme:STATUS>U</fme:STATUS>
            <fme:VARIANT_NAME />
            <fme:MAP_100K>7272</fme:MAP_100K>
            <fme:Place_ID>45880</fme:Place_ID>
            <gml:pointProperty>
                <gml:Point srsName="EPSG:3112" srsDimension="2">
                    <gml:pos>141.625915527344 -12.5836181640625</gml:pos>
                </gml:Point>
            </gml:pointProperty>
        </fme:GML>
    </gml:featureMember>
</gml:FeatureCollection>
</xml>

私の構造体

type FeatureCollection struct {
    Xsi            string   `xml:"xsi,attr"`
    Fme            string   `xml:"fme,attr"`
    Gml            string   `xml:"gml,attr"`
    Xlink          string   `xml:"xlink,attr"`
    LowerCorner    string   `xml:"boundedBy>Envelope>lowerCorner"`
    UpperCorner    string   `xml:"boundedBy>Envelope>upperCorner"`
    Envelope       Envelope `xml:"boundedBy>Envelope"`
    SchemaLocation string   `xml:"schemaLocation,attr"`
    Features       []Feature
}

type Feature struct {
    PlaceID     string `xml:"featureMember>GML>Place_ID"`
    StateID     string `xml:"featureMember>GML>STATE_ID"`
    Postcode    string `xml:"featureMember>GML>POSTCODE"`
    CGDN        string `xml:"featureMember>GML>CGDN"`
    Map100K     string `xml:"featureMember>GML>MAP_100K"`
ETC...
}
4

1 に答える 1

1

XML タグは、(最大で) 1 つの構造体フィールドにのみマップできます。パッケージは、encoding/xmlXML タグごとに、どの構造体フィールドにデコードするかを決定する必要があります。XML をモデル化する構造体は奇妙で、この決定があいまいになります。

たとえば、次の例を見てみましょう。

type FeatureCollection struct {
    ...
    LowerCorner    string   `xml:"boundedBy>Envelope>lowerCorner"`
    UpperCorner    string   `xml:"boundedBy>Envelope>upperCorner"`
    Envelope       Envelope `xml:"boundedBy>Envelope"`
    ...
}

encoding/xmlパッケージは、XML タグ<Envelope>をデコードする場所を決定できませLowerCornerん。にUpperCorner?にEnvelope?はい、LowerCornerは のサブ要素に過ぎないことはわかっています<Envelope>が、要素全体<Envelope>が にマップされているためFeatureCollection.Envelope、これは許可されていません。

LowerCornerおよびUpperCornerフィールドを構造体型に移動する必要がありますEnvelope。これは、それらが属する場所であり、Envelopexml タグ全体を非整列化する必要があるためです (そうでない場合は、FeatureCollection.Envelope完全に削除できます)。したがって、このパターンに従って、フィールドを所属する場所に配置します。

必要なすべての情報を抽出する更新されたモデルを次に示します。

type FeatureCollection struct {
    Xsi            string   `xml:"xsi,attr"`
    Fme            string   `xml:"fme,attr"`
    Gml            string   `xml:"gml,attr"`
    Xlink          string   `xml:"xlink,attr"`
    Envelope       Envelope `xml:"boundedBy>Envelope"`
    SchemaLocation string   `xml:"schemaLocation,attr"`
    FeaturesGML    []GML    `xml:"featureMember>GML"`
}
type Envelope struct {
    SrsName      string `xml:"srsName,attr"`
    SrsDimension string `xml:"srsDimension,attr"`
    LowerCorner  string `xml:"lowerCorner"`
    UpperCorner  string `xml:"upperCorner"`
}
type GML struct {
    ID          string `xml:"id,attr"`
    PlaceID     string `xml:"Place_ID"`
    StateID     string `xml:"STATE_ID"`
    Postcode    string `xml:"POSTCODE"`
    CGDN        string `xml:"CGDN"`
    Map100K     string `xml:"MAP_100K"`
    Point       Point  `xml:"pointProperty>Point"`
    VariantName string `xml:"VARIANT_NAME"`
    RecordID    string `xml:"RECORD_ID"`
    LatSeconds  string `xml:"lat_seconds"`
    Status      string `xml:"STATUS"`
    LongSeconds string `xml:"long_seconds"`
    ConciseGAZ  string `xml:"CONCISE_GAZ"`
    Lattitude   string `xml:"LATITUDE"`
    AuthorityID string `xml:"AUTHORITY_ID"`
    Longitude   string `xml:"LONGITUDE"`
    LongMinutes string `xml:"long_minutes"`
    LatDegrees  string `xml:"lat_degrees"`
    NAME        string `xml:"NAME"`
    LatMinutes  string `xml:"lat_minutes"`
    ObjectID    string `xml:"OBJECTID"`
    FeatCode    string `xml:"FEAT_CODE"`
    LongDegrees string `xml:"long_degrees"`
}
type Point struct {
    SrsName      string `xml:"srsName,attr"`
    SrsDimension string `xml:"srsDimension,attr"`
    Pos          string `xml:"pos"`
}

エラーなしで実行されるGo Playgroundのコードの修正版を次に示します。

XML から非整列化されたすべての情報が構造体に保持されていることを確認するには、次のようにします。

fmt.Printf("%+v", v)

出力:

&{ Xsi: http://www.w3.org/2001/XMLSchema-instance Fme: http://www.safe.com/gml/fme Gml: http://www.opengis.net/gml Xlink: http ://www.w3.org/1999/xlinkエンベロープ:{SrsName:EPSG:3112 SrsDimension: 2tblMainGML.xsd FeaturesGML:[{ID:id5255fa48-42b3-43d1-9e0d-b2ba8b57a936 PlaceID:45880 StateID:QLD 郵便番号: CGDN:N Map100K:7272 Point:{SrsName:EPSG:3112 SrsDimension:2 Pos:141.625915527344}ariant834-61256. : RecordID:QLD48234 LatSeconds:0 Status:U LongSeconds:32 ConciseGAZ:N Lattitude:-12.58361 AuthorityID:QLD Longitude:141.62583 LongMinutes:37 LatDegrees:-12 NAME:HATCHMAN POINT LatMinutes:35 ObjectID:1 FeatCode:PT LongDegrees:141} ]}

于 2016-04-11T07:36:18.960 に答える