猿问

解组多个 XML 项

我试图解组包含在具有相同结构的节点中的多个项目以供进一步处理,但似乎无法访问数据,我不知道为什么。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)

    }

}

运行时输出为空,这让我认为数据没有被解组。输出是:


{[]}


天涯尽头无女友
浏览 137回答 1
1回答

慕的地6264312

您忽略了从xml.Unmarshal. 通过稍微修改您的程序,我们可以看到发生了什么:xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nil而在文档中闲逛,我们发现,在默认情况下的包只支持XML的UTF-8编码:&nbsp; &nbsp; // CharsetReader, if non-nil, defines a function to generate&nbsp; &nbsp; // charset-conversion readers, converting from the provided&nbsp; &nbsp; // non-UTF-8 charset into UTF-8. If CharsetReader is nil or&nbsp; &nbsp; // returns an error, parsing stops with an error. One of the&nbsp; &nbsp; // the CharsetReader's result values must be non-nil.&nbsp; &nbsp; CharsetReader func(charset string, input io.Reader) (io.Reader, error)因此,您似乎需要提供自己的字符集转换例程。您可以通过像这样修改代码来注入它:decoder := xml.NewDecoder(xmlFile)decoder.CharsetReader = makeCharsetReadererr := decoder.Decode(&tides)(请注意,我们现在正在从一个io.Reader而不是字节数组解码,因此ReadAll可以删除逻辑)。该golang.org/x/text/encoding套餐的家庭可能会帮助您实施makeCharsetReader功能。像这样的事情可能会奏效:import "golang.org/x/text/encoding/charmap"func makeCharsetReader(charset string, input io.Reader) (io.Reader, error) {&nbsp; &nbsp; if charset == "ISO-8859-1" {&nbsp; &nbsp; &nbsp; &nbsp; // Windows-1252 is a superset of ISO-8859-1, so should do here&nbsp; &nbsp; &nbsp; &nbsp; return charmap.Windows1252.NewDecoder().Reader(input), nil&nbsp; &nbsp; }&nbsp; &nbsp; return nil, fmt.Errorf("Unknown charset: %s", charset)}然后您应该能够解码 XML。
随时随地看视频慕课网APP

相关分类

Go
我要回答