【发布时间】:2016-03-29 17:18:07
【问题描述】:
在使用 Go 解析 XML 时,如何将嵌套元素的属性直接读取到我的结构中?
我的 XML 如下所示。它是 OpenStreetMap 格式的一部分:
<way id="123" >
<nd ref="101"/>
<!-- Lots of nd elements repeated -->
<nd ref="109"/>
</way>
我有
type Way struct {
Nodes []NodeRef `xml:"nd"`
}
与
type NodeRef struct {
Ref int `xml:"ref,attr"`
}
但我希望能够做到
type Way struct {
Nodes []int `???`
}
直接。
Unmarshalling 上的文档对我没有帮助。我尝试过使用xml:"nd>ref,attr",但失败并显示“chain not valid with attr flag”。
请看下面的示例代码。 Run the code in Go Playground
package main
import (
"encoding/xml"
"fmt"
"io"
"os"
"strings"
)
func main() {
data := `
<way id="5090250" >
<nd ref="822403"/>
<nd ref="21533912"/>
<nd ref="821601"/>
<nd ref="21533910"/>
<nd ref="135791608"/>
<nd ref="333725784"/>
<nd ref="333725781"/>
<nd ref="333725774"/>
<nd ref="333725776"/>
<nd ref="823771"/>
</way>
`
r := strings.NewReader(data)
way, err := ReadWay(r)
if err != nil {
fmt.Println("Could not read", err)
os.Exit(1)
}
fmt.Println(way)
}
// I'd like to get rid of this nested struct.
type NodeRef struct {
Ref int `xml:"ref,attr"`
}
type Way struct {
ID int `xml:"id,attr"`
// How can I write all <nd ref="123"/> directly into Nodes []int?
Nodes []NodeRef `xml:"nd"`
}
func ReadWay(reader io.Reader) (Way, error) {
var way Way
if err := xml.NewDecoder(reader).Decode(&way); err != nil {
return way, err // Why can't I return nil, err?
}
return way, nil
}
【问题讨论】:
标签: xml go xml-parsing openstreetmap