tss/exp/xml/xml.go
2022-04-26 09:55:59 -04:00

89 lines
1.4 KiB
Go

package main
import (
"encoding/csv"
"encoding/xml"
"fmt"
"log"
"os"
"strings"
"time"
)
/*
XMLName xml.Name `xml:"person"`
FirstName string `xml:"firstname"`
Parents interface{} `xml:"parent"`
*/
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Author string `xml:"author"`
Guid string `xml:"guid"`
Description string `xml:"description"`
Content string `xml:",innerxml"`
}
func main() {
fmt.Println("hello go")
data, err := os.ReadFile("./ounapuu.xml")
if err != nil {
log.Fatal(err)
}
v := struct {
Items []Item `xml:"channel>item"`
}{}
err = xml.Unmarshal(data, &v)
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("./data.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()
if err != nil {
log.Fatal(err)
}
csvw := csv.NewWriter(f)
csvw.Write([]string{
"date",
"link",
"info",
})
for index, item := range v.Items {
fmt.Println(index, item.Title)
//fmt.Println(item.Content)
c := item.Content
csvw.Write([]string{
time.Now().String(),
item.Link,
strings.Replace(c, "\n", " ", -1),
})
}
}
func foo() {
x := `
<foo>
<bar><blue>slkdfjdslk</blue></bar>
</foo>
`
s := struct {
Bar struct {
Content string `xml:",innerxml"`
} `xml:"bar"`
}{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
log.Fatal(err)
}
fmt.Println(s.Bar.Content)
}