go语言解析xml

前端之家收集整理的这篇文章主要介绍了go语言解析xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
studygolang.xml

<?xml version="1.0" encoding="utf-8"?>

<persons>
    <person name="polaris" age="28">
        <career>无业游民</career>
        <interests>
            <interest>编程</interest>
            <interest>下棋</interest>
        </interests>
    </person>
</persons>



package main

import (
"encoding/xml"
"io/IoUtil"
"log"
)
type Result struct {
Person []Person `xml:"person"`
}
type Person struct {
Name      string    `xml:"name,attr"`
Age       int       `xml:"age,attr"`
Career    string    `xml:"career"`
Interests Interests `xml:"interests"`
}
type Interests struct {
Interest []string `xml:"interest"`
}
func (person *Person) Chkis18() (flag bool) {
if person.Age > 18 {
flag = true
}
return flag
}
type Checker interface {
Chkis18() (flag bool)
}
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)
}
A := "AAAA"
log.Println(result.Person)
}
原文链接:https://www.f2er.com/xml/298404.html

猜你在找的XML相关文章