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)
}
 

你可能感兴趣的:(go语言解析xml)