亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

XML 解析返回帶有換行符的字符串

XML 解析返回帶有換行符的字符串

Go
MM們 2023-06-05 17:04:27
我正在嘗試通過站點地圖解析 XML,然后遍歷地址以獲取 Go 中帖子的詳細信息。但是我收到了這個奇怪的錯誤:: URL 中的第一個路徑段不能包含冒號這是代碼片段:type SitemapIndex struct {    Locations []Location `xml:"sitemap"`}type Location struct {    Loc string `xml:"loc"`}func (l Location) String() string {    return fmt.Sprintf(l.Loc)}func main() {    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")    bytes, _ := ioutil.ReadAll(resp.Body)    var s SitemapIndex    xml.Unmarshal(bytes, &s)    for _, Location := range s.Locations {        fmt.Printf("Location: %s", Location.Loc)        resp, err := http.Get(Location.Loc)        fmt.Println("resp", resp)        fmt.Println("err", err)    }}輸出:Location: https://www.washingtonpost.com/news-sitemaps/politics.xmlresp <nil>err parse https://www.washingtonpost.com/news-sitemaps/politics.xml: first path segment in URL cannot contain colonLocation: https://www.washingtonpost.com/news-sitemaps/opinions.xmlresp <nil>err parse https://www.washingtonpost.com/news-sitemaps/opinions.xml: first path segment in URL cannot contain colon......我的猜測是Location.Loc在實際地址之前和之后返回一個新行。例如:\nLocation: https://www.washingtonpost.com/news-sitemaps/politics.xml\n因為硬編碼 URL 按預期工作:for _, Location := range s.Locations {        fmt.Printf("Location: %s", Location.Loc)        test := "https://www.washingtonpost.com/news-sitemaps/politics.xml"        resp, err := http.Get(test)        fmt.Println("resp", resp)        fmt.Println("err", err)    }但是我是 Go 的新手,所以我不知道出了什么問題。你能告訴我我哪里錯了嗎?
查看完整描述

2 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

您確實是對的,問題來自換行符。如您所見,您在使用時Printf沒有添加任何內容\n,并且在輸出的開頭添加了一個,在輸出的結尾添加了一個。

您可以使用strings.Trim刪除這些換行符。這是一個使用您嘗試解析的站點地圖的示例。修剪字符串后,您將能夠http.Get毫無錯誤地調用它。

func main() {

? ? var s SitemapIndex

? ? xml.Unmarshal(bytes, &s)


? ? for _, Location := range s.Locations {

? ? ? ? loc := strings.Trim(Location.Loc, "\n")

? ? ? ? fmt.Printf("Location: %s\n", loc)

? ? }

}

如預期的那樣,此代碼正確輸出沒有任何換行符的位置:


Location: https://www.washingtonpost.com/news-sitemaps/politics.xml

Location: https://www.washingtonpost.com/news-sitemaps/opinions.xml

Location: https://www.washingtonpost.com/news-sitemaps/local.xml

Location: https://www.washingtonpost.com/news-sitemaps/sports.xml

Location: https://www.washingtonpost.com/news-sitemaps/national.xml

Location: https://www.washingtonpost.com/news-sitemaps/world.xml

Location: https://www.washingtonpost.com/news-sitemaps/business.xml

Location: https://www.washingtonpost.com/news-sitemaps/technology.xml

Location: https://www.washingtonpost.com/news-sitemaps/lifestyle.xml

Location: https://www.washingtonpost.com/news-sitemaps/entertainment.xml

Location: https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml

字段中有這些換行符的原因Location.Loc是此 URL 返回的 XML。條目遵循這種形式:


<sitemap>

<loc>

https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml

</loc>

</sitemap>

正如您所看到的,元素中的內容前后都有換行符loc。


查看完整回答
反對 回復 2023-06-05
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

查看修改代碼中嵌入的注釋以描述和修復問題


func main() {

    resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")

    bytes, _ := ioutil.ReadAll(resp.Body)

    var s SitemapIndex

    xml.Unmarshal(bytes, &s)

    for _, Location := range s.Locations {

            // Note that %v shows that there are indeed newlines at beginning and end of Location.Loc

            fmt.Printf("Location: (%v)", Location.Loc)

            // solution: use strings.TrimSpace to remove newlines from Location.Loc

            resp, err := http.Get(strings.TrimSpace(Location.Loc))

            fmt.Println("resp", resp)

            fmt.Println("err", err)

    }

}


查看完整回答
反對 回復 2023-06-05
  • 2 回答
  • 0 關注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號