package main
import (
"encoding/xml"
//"fmt"
//"strings"
"io/ioutil"
"log"
)
type Result struct {
Person []Person
}
type Person struct {
Name string
Age int
Career string
Interests Interests
}
type Interests struct {
Interest []string
}
func main() {
content, err := ioutil.ReadFile("studygolang.xml")
if err != nil {
log.Fatal(err)
}
var result Result
err = xml.Unmarshal(content, &result)
if err != nil {
log.Fatal(err)
}
log.Println(result)
}
/*
func main() {
var t xml.Token
var err error
input := `<Person><FirstName>Xu</FirstName><LastName>Xinhua</LastName></Person>`
inputReader := strings.NewReader(input)
// 從文件讀取,如可以如下:
// content, err := ioutil.ReadFile("studygolang.xml")
// decoder := xml.NewDecoder(bytes.NewBuffer(content))
decoder := xml.NewDecoder(inputReader)
for t, err = decoder.Token(); err == nil; t, err = decoder.Token() {
switch token := t.(type) {
case xml.StartElement:
name := token.Name.Local
fmt.Printf("Token name: %s\n", name)
for _, attr := range token.Attr {
attrName := attr.Name.Local
attrValue := attr.Value
fmt.Printf("An attribute is: %s %s\n", attrName, attrValue)
}
case xml.EndElement:
fmt.Printf("Token of '%s' end\n", token.Name.Local)
case xml.CharData:
content := string([]byte(token))
fmt.Printf("This is the content: %v\n", content)
default:
// ...
}
}
}
*/
GO: 實(shí)例 t_xml.go
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- From Washington, this is VOA news. I'm Jee Abbey Lee repo...
- JPS列出當(dāng)前Java進(jìn)程-q 只顯示進(jìn)程ID-v 顯示JVM啟動參數(shù)-m 顯示傳給main的參數(shù)-l 顯示主類的...