1 回答

TA贡献2039条经验 获得超8个赞
一个 XML 标签只能映射到(最多)一个结构域。该encoding/xml包必须决定每个XML标签的struct场将被解码。您对 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: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 ]}
- 1 回答
- 0 关注
- 230 浏览
添加回答
举报