我试图解组包含在具有相同结构的节点中的多个项目以供进一步处理,但似乎无法访问数据,我不知道为什么。XML 数据的结构如下所示(我正在尝试访问所有Item的 :
<?xml version="1.0" encoding="ISO-8859-1" ?>
<datainfo>
<origin>NOAA/NOS/CO-OPS</origin>
<producttype> Annual Tide Prediction </producttype>
<IntervalType>High/Low Tide Predictions</IntervalType>
<data>
<item>
<date>2015/12/31</date>
<day>Thu</day>
<time>03:21 AM</time>
<predictions_in_ft>5.3</predictions_in_ft>
<predictions_in_cm>162</predictions_in_cm>
<highlow>H</highlow>
</item>
<item>
<date>2015/12/31</date>
<day>Thu</day>
<time>09:24 AM</time>
<predictions_in_ft>2.4</predictions_in_ft>
<predictions_in_cm>73</predictions_in_cm>
<highlow>L</highlow>
</item>
</data>
</datainfo>
我的代码是:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
// TideData stores a series of tide predictions
type TideData struct {
Tides []Tide `xml:"data>item"`
}
// Tide stores a single tide prediction
type Tide struct {
Date string `xml:"date"`
Day string `xml:"day"`
Time string `xml:"time"`
PredictionFt float64 `xml:"predictions_in_ft"`
PredictionCm float64 `xml:"predictions_in_cm"`
HighLow string `xml:"highlow"`
}
func (t Tide) String() string {
return t.Date + " " + t.Day + " " + t.Time + " " + t.HighLow
}
func main() {
xmlFile, err := os.Open("9414275 Annual.xml")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
var tides TideData
xml.Unmarshal(b, &tides)
fmt.Println(tides)
for _, datum := range tides.Tides {
fmt.Printf("\t%s\n", datum)
}
}
运行时输出为空,这让我认为数据没有被解组。输出是:
{[]}
慕的地6264312
相关分类