-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prac11_Basics_parsingXML.go
116 lines (100 loc) · 2.61 KB
/
Prac11_Basics_parsingXML.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
)
/*
//The sitemap we are going to parse
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/politics.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/opinions.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/local.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/sports.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/national.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/world.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/business.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/technology.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/lifestyle.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/entertainment.xml
</loc>
</sitemap>
<sitemap>
<loc>
https://www.washingtonpost.com/news-sitemaps/goingoutguide.xml
</loc>
</sitemap>
</sitemapindex>
*/
//encoding/xml is to stucture data as xml
//defining structure
type SitemapIndex struct {
//we created an array of Location type struct
//L of location must be capital so that export to xml works, its syntax man
Locations []Location `xml:"sitemap"`
}
type Location struct {
//loc must be lower case as tag is itself lowercase in sitemao
Loc string `xml:"loc"`
}
//overriding string class with our own method to use here, getting everything only as strings not struct
func (l Location) String() string {
//we use a value receiver to get a string value from xml location tag with this function
//Sprintf formats strings for us
return fmt.Sprint(l.Loc)
}
func main() {
resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
var s SitemapIndex
xml.Unmarshal(bytes, &s)
fmt.Println(s.Locations)
}
//anything with square brackets and number in it is an array, anything without is a slice. arrays are fixed, slices aren't
//e.g arr [5] is array of int type
//e.g arr [] is slice of whatever type it is assigned to
//e.g specifying a slice =>arr:=[]string{"helo","heloo"} //this is obviously within a function// for global use var keyword
//e.g specifying an array=> arr:=[2]string{"hel","o"}
// I read about arrays and slices man, trust me use slices, you can even slice an array like this
// a:=[5]int{1,2,3,4,5},
// var b []int=a[1:4] //slices numbers 2,3,4