省略名称将嵌入到自身。
例如:
package main
import "fmt"
type Animal struct {
Name string
}
type Cat struct {
Animal
}
type Dog struct {
A Animal
}
func main() {
cat := Cat{Animal{"Kitty"}}
fmt.Println(cat.Name)
fmt.Println(cat.Animal.Name)
dog := Dog{Animal{"Snoopy"}}
fmt.Println(dog.A.Name)
}
Go Playground
你的情况
看起来
已经足够了(其他人也一样)。
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
)
type News struct {
Publishdate string `xml:"news>publication_date"`
Title string `xml:"news>title"`
Summary string `xml:"news>keywords"`
}
type urlset struct {
XMLName xml.Name `xml:"urlset"`
URL []struct {
Loc string `xml:"loc"`
News `xml:"news"`
} `xml:"url"`
}
func getXML(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return []byte{}, fmt.Errorf("GET error: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return []byte{}, fmt.Errorf("Status error: %v", resp.StatusCode)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("Read body: %v", err)
}
return data, nil
}
func main() {
var URLset urlset
xmlBytes := []byte(`
<urlset>
<url>
<loc>https://www.dw.com/de/kopf-an-kopf-rennen-bei-parlamentswahl-in-australien/a-61887162</loc>
<news:news>
<news:publication>
<news:name>Deutsche Welle</news:name>
<news:language>de</news:language>
</news:publication>
<news:publication_date>2022-05-21T11:28:55.875Z</news:publication_date>
<news:title>Kopf-an-Kopf-Rennen bei Parlamentswahl in Australien</news:title>
<news:keywords>Australien,Parlamentswahl,Scott Morrison,Anthony Albanese,Labor-Partei,Liberale</news:keywords>
</news:news>
<image:image>
<image:loc>https://static.dw.com/image/61872101_403.jpg</image:loc>
<image:caption>Der australische Premierminister Scott Morrison (r.) und sein Herausforderer, Oppositionsführer Anthony Albanese</image:caption>
</image:image>
</url>
<url>
<loc>https://www.dw.com/de/ukraine-aktuell-selenskyj-verlangt-entsch%C3%A4digungsfonds/a-61885143</loc>
<news:news>
<news:publication>
<news:name>Deutsche Welle</news:name>
<news:language>de</news:language>
</news:publication>
<news:publication_date>2022-05-21T11:10:21.813Z</news:publication_date>
<news:title>Ukraine aktuell: Selenskyj verlangt Entschädigungsfonds</news:title>
<news:keywords>Ukraine,Krieg,Russland,Wolodymyr Selenskyj,Wladimir Putin,Mariupol</news:keywords>
</news:news>
<image:image>
<image:loc>https://static.dw.com/image/61885205_403.jpg</image:loc>
<image:caption>75. Filmfestival Cannes | Rede von Wolodymyr Selenskyj</image:caption>
</image:image>
</url>
<urlset>
`)
xml.Unmarshal(xmlBytes, &URLset)
for _, URLElement := range URLset.URL {
fmt.Println(
"[Element]:",
"\nTitle #", URLElement.Title,
"\nPublicationDate #", URLElement.Publishdate,
"\nSummary#", URLElement.Summary,
"\nLoc #", URLElement.Loc, "\n")
}
}
关于XML命名空间
更多例子
- ExampleUnmarshal 这个链接来自于
go/src/encoding/xml/example_test.go
实际上,所有的例子都很容易理解。所以这对学习很有帮助。
News
结构体类型。然后,要嵌入该字段,您只需声明该字段不带显式字段名称,即仅使用字段的类型声明该字段。但这将使Go结构与XML不兼容,因此您需要在提升字段的标记中添加news>
。另一种选择是在URL结构中声明所有字段,并在标记中使用elem1>elem2...
来指导解组器。 - mkopriva