我在 Go 中解析 xml 时遇到问题。
一个只有一个“游戏”的 xml 示例:
<root>
<game>
<id>z0046-54</id>
<creation_time>1612988348</creation_time>
<black>aivo</black>
<white>gosha</white>
<turn_color>white</turn_color>
<board>h8 h9 i6 i8 j6 g9 j9 j7 k6 l6 g10 k7 l7 i10 i9 g6 j10 k11 j5 i4 k4 l3 h5 m5 j8 j11 g8 h7 i7 l5 k9 h6 m7 k12 l13 n6 m8 n9 m9</board>
<moves>h8 swap h9 i6 swap i8 swap j6 g9 j9 j7 k6 l6 g10 k7 l7 i10 i9 g6 j10 k11 j5 i4 k4 l3 h5 m5 j8 j11 g8 h7 i7 l5 k9 h6 m7 k12 l13 n6 m8 n9 m9</moves>
<alt5></alt5>
<proposition></proposition>
<proposer></proposer>
<status>finished</status>
<rule>taraguchi10</rule>
<time>7776000</time>
<tpm>2592000</tpm>
<start_time>1612988348</start_time>
<end_time>1613736768</end_time>
<winner>black</winner>
<winby>resign</winby>
<time_left_black>7536372</time_left_black>
<time_left_white>7267413</time_left_white>
<tid>z0046</tid>
</game>
</root>
我想从 xml 获取游戏切片。带有游戏和所有文件结构的代码:
package data
type ro struct {
RoGames []roGame `xml:"root>game"`
}
type roGame struct {
Id int `xml:"game>id"`
Black string `xml:"game>black"`
White string `xml:"game>white"`
Moves string `xml:"game>moves"`
Alt5 string `xml:"game>alt_5"`
Rule string `xml:"game>rule"`
Winner string `xml:"game>winner"`
}
另一个文件:
package data
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
func Xml() {
xmlFile, err := os.Open("data.xml")
if err != nil {
panic(err)
}
fmt.Println("Successfully Opened xml")
defer xmlFile.Close()
inBytes, err := ioutil.ReadAll(xmlFile)
if err != nil {
panic(err)
}
var ro ro
xml.Unmarshal(inBytes, &ro)
fmt.Println(ro)
}
我从主包调用“Xml”函数并获取此输出:
Successfully Opened xml
{[]}
我认为解析过程是可以的,这是sctructs的问题。
MYYA
相关分类