gpt4 book ai didi

xml - 解码多个 XML 项

转载 作者:IT王子 更新时间:2023-10-29 01:52:47 31 4
gpt4 key购买 nike

我正在尝试解码具有相同结构的节点中包含的多个项目以进行进一步处理,但似乎无法访问数据,我也不确定为什么。 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)
}
}

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

{[]}

最佳答案

您正在忽略从 xml.Unmarshal 返回的错误。通过 slightly modifying your program ,我们可以看到发生了什么:

xml: encoding "ISO-8859-1" declared but Decoder.CharsetReader is nil

poking around in the documentation ,我们发现默认情况下该包仅支持以 UTF-8 编码的 XML:

    // CharsetReader, if non-nil, defines a function to generate
// charset-conversion readers, converting from the provided
// non-UTF-8 charset into UTF-8. If CharsetReader is nil or
// returns an error, parsing stops with an error. One of the
// the CharsetReader's result values must be non-nil.
CharsetReader func(charset string, input io.Reader) (io.Reader, error)

看来你需要提供自己的字符集转换例程。您可以通过像这样修改代码来注入(inject)它:

decoder := xml.NewDecoder(xmlFile)
decoder.CharsetReader = makeCharsetReader
err := decoder.Decode(&tides)

(请注意,我们现在从 io.Reader 而不是字节数组进行解码,因此可以删除 ReadAll 逻辑)。 golang.org/x/text/encoding family of packages可能会帮助您实现 makeCharsetReader 功能。这样的事情可能会起作用:

import "golang.org/x/text/encoding/charmap"

func makeCharsetReader(charset string, input io.Reader) (io.Reader, error) {
if charset == "ISO-8859-1" {
// Windows-1252 is a superset of ISO-8859-1, so should do here
return charmap.Windows1252.NewDecoder().Reader(input), nil
}
return nil, fmt.Errorf("Unknown charset: %s", charset)
}

然后您应该能够解码 XML。

关于xml - 解码多个 XML 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712015/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com