猿问

使用 golang encoding/xml 解组 GML 时出错

我正在尝试解组一些 XML,实际上是地理标记语言 (GML)。


我在http://play.golang.org/p/qS6GjCOtHF有一个例子


两个问题,第一个:


错误读取 xml main.FeatureCollection 字段“LowerCorner”与标签“boundedBy>Envelope>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>

                </gml:Point>

            </gml:pointProperty>

        </fme:GML>

    </gml:featureMember>

</gml:FeatureCollection>

</xml>


慕妹3242003
浏览 231回答 1
1回答

largeQ

一个 XML 标签只能映射到(最多)一个结构域。该encoding/xml包必须决定每个XML标签的struct场将被解码。您对 XML 建模的结构很奇怪,并使这个决定模棱两可。例如,让我们以这个例子为例:type FeatureCollection struct {&nbsp; &nbsp; ...&nbsp; &nbsp; LowerCorner&nbsp; &nbsp; string&nbsp; &nbsp;`xml:"boundedBy>Envelope>lowerCorner"`&nbsp; &nbsp; UpperCorner&nbsp; &nbsp; string&nbsp; &nbsp;`xml:"boundedBy>Envelope>upperCorner"`&nbsp; &nbsp; Envelope&nbsp; &nbsp; &nbsp; &nbsp;Envelope `xml:"boundedBy>Envelope"`&nbsp; &nbsp; ...}该encoding/xml包不能决定在XML标签<Envelope>应被解码成,例如,进入LowerCorner?进UpperCorner? 进Envelope? 是的,我知道LowerCorner只是 的一个子元素,<Envelope>但由于整个<Envelope>元素都映射到FeatureCollection.Envelope,这是不允许的。您应该将LowerCorner和UpperCorner字段移动到您的Envelope结构类型中,因为这是它们所属的地方,并且您想要解组整个Envelopexml 标记(或者如果没有,FeatureCollection.Envelope可以完全删除)。因此,请遵循此模式将字段放置到它们所属的位置。这是您更新的模型,它提取了您想要的所有信息:type FeatureCollection struct {&nbsp; &nbsp; Xsi&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp;`xml:"xsi,attr"`&nbsp; &nbsp; Fme&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp;`xml:"fme,attr"`&nbsp; &nbsp; Gml&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp;`xml:"gml,attr"`&nbsp; &nbsp; Xlink&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp;`xml:"xlink,attr"`&nbsp; &nbsp; Envelope&nbsp; &nbsp; &nbsp; &nbsp;Envelope `xml:"boundedBy>Envelope"`&nbsp; &nbsp; SchemaLocation string&nbsp; &nbsp;`xml:"schemaLocation,attr"`&nbsp; &nbsp; FeaturesGML&nbsp; &nbsp; []GML&nbsp; &nbsp; `xml:"featureMember>GML"`}type Envelope struct {&nbsp; &nbsp; SrsName&nbsp; &nbsp; &nbsp; string `xml:"srsName,attr"`&nbsp; &nbsp; SrsDimension string `xml:"srsDimension,attr"`&nbsp; &nbsp; LowerCorner&nbsp; string `xml:"lowerCorner"`&nbsp; &nbsp; UpperCorner&nbsp; string `xml:"upperCorner"`}type GML struct {&nbsp; &nbsp; ID&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string `xml:"id,attr"`&nbsp; &nbsp; PlaceID&nbsp; &nbsp; &nbsp;string `xml:"Place_ID"`&nbsp; &nbsp; StateID&nbsp; &nbsp; &nbsp;string `xml:"STATE_ID"`&nbsp; &nbsp; Postcode&nbsp; &nbsp; string `xml:"POSTCODE"`&nbsp; &nbsp; CGDN&nbsp; &nbsp; &nbsp; &nbsp; string `xml:"CGDN"`&nbsp; &nbsp; Map100K&nbsp; &nbsp; &nbsp;string `xml:"MAP_100K"`&nbsp; &nbsp; Point&nbsp; &nbsp; &nbsp; &nbsp;Point&nbsp; `xml:"pointProperty>Point"`&nbsp; &nbsp; VariantName string `xml:"VARIANT_NAME"`&nbsp; &nbsp; RecordID&nbsp; &nbsp; string `xml:"RECORD_ID"`&nbsp; &nbsp; LatSeconds&nbsp; string `xml:"lat_seconds"`&nbsp; &nbsp; Status&nbsp; &nbsp; &nbsp; string `xml:"STATUS"`&nbsp; &nbsp; LongSeconds string `xml:"long_seconds"`&nbsp; &nbsp; ConciseGAZ&nbsp; string `xml:"CONCISE_GAZ"`&nbsp; &nbsp; Lattitude&nbsp; &nbsp;string `xml:"LATITUDE"`&nbsp; &nbsp; AuthorityID string `xml:"AUTHORITY_ID"`&nbsp; &nbsp; Longitude&nbsp; &nbsp;string `xml:"LONGITUDE"`&nbsp; &nbsp; LongMinutes string `xml:"long_minutes"`&nbsp; &nbsp; LatDegrees&nbsp; string `xml:"lat_degrees"`&nbsp; &nbsp; NAME&nbsp; &nbsp; &nbsp; &nbsp; string `xml:"NAME"`&nbsp; &nbsp; LatMinutes&nbsp; string `xml:"lat_minutes"`&nbsp; &nbsp; ObjectID&nbsp; &nbsp; string `xml:"OBJECTID"`&nbsp; &nbsp; FeatCode&nbsp; &nbsp; string `xml:"FEAT_CODE"`&nbsp; &nbsp; LongDegrees string `xml:"long_degrees"`}type Point struct {&nbsp; &nbsp; SrsName&nbsp; &nbsp; &nbsp; string `xml:"srsName,attr"`&nbsp; &nbsp; SrsDimension string `xml:"srsDimension,attr"`&nbsp; &nbsp; Pos&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 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:2 LowerCorner:45.2921142578125 -80.2166748046875 UpperCorner:169.000122070313 -9.14251708984375} SCHEMALOCATION:http://www.safe.com/gml/fme tblMainGML.xsd FeaturesGML:[{ID:id5255fa48-42b3-43d1-9e0d-b2ba8b57a936 PlaceID:45880 STATEID:QLD邮编:CGDN:N Map100K:7272点:{SrsName:EPSG:3112 SrsDimension:2个位置:141.625915527344 -12.5836181640625} VariantName : 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 LatDegrees:1Det1Dets LongSeconds:N Lattitude:-12.58361 ]}
随时随地看视频慕课网APP

相关分类

Go
我要回答