I'm parsing XML which contains URLs and I want to iterate over this XML to get all URLs and make a request to each URL, but the strings contain new line character \n
. How can I avoid this new lines in URL?
Go version is go1.12.7 darwin/amd64. I have solution for this problem I just removing this characters from string.
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}
type NewsMap struct {
Keyword string
Location string
}
type News struct {
Titles []string `xml:"url>news>title"`
Keywords []string `xml:"url>news>keywords"`
Locations []string `xml:"url>loc"`
}
func main() {
var s SitemapIndex
var n News
newsMap := make(map[string]NewsMap)
resp, _ := http.Get("https://washingtonpost.com/news-sitemaps/index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &s)
for _, Location := range s.Locations {
tempURL := strings.Replace(Location, "n", "", -1) // how to avoid new lines character in url?
resp, err := http.Get(tempURL)
// do some stuff...
}
Without this replace method on Location Im getting an error
parse
https://www.washingtonpost.com/news-sitemaps/politics.xml
: net/url: invalid control character in URL
exit status 1
Here is example XML file https://www.washingtonpost.com/news-sitemaps/politics.xml