Golang,解析XML网站

发布于 2025-02-13 16:31:43 字数 732 浏览 0 评论 0原文

当我尝试解析XML站点时,对我无济于事,输出“ []”。结果,我决定下载本网站的XML文件,然后删除后 <?XML版本=“ 1.0” encoding =“ Windows-1251”?>,然后一切都对我有用。是否可以以某种方式读取数据而不删除此片段?

type ValCurs struct {
XMLName xml.Name `xml:"ValCurs"`
Date    string   `xml:"Date, attr"`
Name    string   `xml:"name, attr"`
Valute  []Valute `xml:"Valute"`
}

type Valute struct {
XMLName  xml.Name `xml:"Valute"`
CharCode string   `xml:"CharCode"`
Nominal  string   `xml:"Nominal"`
Name     string   `xml:"Name"`
Value    string   `xml:"Value"`
}

func main() {

resp, _ := http.Get("http://www.cbr.ru/scripts/XML_daily.asp")
defer resp.Body.Close()
req, _ := ioutil.ReadAll(resp.Body)
var val ValCurs

xml.Unmarshal(req, &val)
fmt.Println(val)
}

When I try to parse the xml site, nothing works for me and output "[]" . As a result, I decided to download the xml file of this site, and after I remove
<?xml version="1.0" encoding="windows-1251"?>, then everything works out for me. Is it possible to somehow read the data without deleting this fragment?

type ValCurs struct {
XMLName xml.Name `xml:"ValCurs"`
Date    string   `xml:"Date, attr"`
Name    string   `xml:"name, attr"`
Valute  []Valute `xml:"Valute"`
}

type Valute struct {
XMLName  xml.Name `xml:"Valute"`
CharCode string   `xml:"CharCode"`
Nominal  string   `xml:"Nominal"`
Name     string   `xml:"Name"`
Value    string   `xml:"Value"`
}

func main() {

resp, _ := http.Get("http://www.cbr.ru/scripts/XML_daily.asp")
defer resp.Body.Close()
req, _ := ioutil.ReadAll(resp.Body)
var val ValCurs

xml.Unmarshal(req, &val)
fmt.Println(val)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2025-02-20 16:31:43

好吧,也许我已经弄清楚了,但是我不确定我是否做得正确。
我安装了Charmap软件包“ github.com/aglyzov/charmap”
并切断BYTS

res1 := bytes.Trim(byts, "<? ?>")

之后,一切都按照我想要的

Okay, maybe I figured it out, but I'm not sure if I did everything right.
I installed charmap package "github.com/aglyzov/charmap"
And cut off the byts

res1 := bytes.Trim(byts, "<? ?>")

After that everything works as I would like

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文