Sunday, 10 January 2016

XML : Unmarshal multiple XML items

I am trying to unmarshal multiple items contained in nodes with an identical structure for further processing, but don't seem to be able to access the data and I am not sure why. The XML data is structured in the following form (I am trying to access all of the Item's:

<?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>

My code is:

  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)      }  }    

When run the output is empty, which leads me to think that the data is not unmarshalled. Output is:

{[]}

No comments:

Post a Comment