小编use*_*204的帖子

通过io/ioutil net/http访问后在Go中解析XML

知道为什么这种解析在直接从站点访问 XML 时不起作用,而当我将其复制并粘贴到 var 时它起作用吗?

package main

import (
  "encoding/xml"
  "fmt"
  "strings"
  "io/ioutil"
  "net/http"
)

type Sitemapindex struct {
  Locations []Location `xml:"channel>item"`
}

type Location struct {
  Loc string `xml:"title"`
}

func (e Location) String () string {
  return fmt.Sprintf(e.Loc)
}

func main() {
  resp, _ := http.Get("https://www.sec.gov/Archives/edgar/xbrlrss.all.xml")
  bytes, _ := ioutil.ReadAll(resp.Body)  
  string_body := string(bytes)  
  var s Sitemapindex
  decoder := xml.NewDecoder(strings.NewReader(string_body))
  decoder.Strict = false
  decoder.Decode(&s)
  fmt.Println(s)
}
Run Code Online (Sandbox Code Playgroud)

xml http go

5
推荐指数
1
解决办法
87
查看次数

标签 统计

go ×1

http ×1

xml ×1