📌  相关文章
📜  golang parsing xml - Go 编程语言代码示例

📅  最后修改于: 2022-03-11 14:45:00.707000             🧑  作者: Mango

代码示例1
package main

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
)

type Data struct {
    XMLName    xml.Name `xml:"data" json:"-"`
    PersonList []Person `xml:"person" json:"people"`
}

type Person struct {
    XMLName   xml.Name `xml:"person" json:"-"`
    Firstname string   `xml:"firstname" json:"firstname"`
    Lastname  string   `xml:"lastname" json:"lastname"`
    Address   *Address `xml:"address" json:"address,omitempty"`
}

type Address struct {
    City  string `xml:"city" json:"city,omitempty"`
    State string `xml:"state" json:"state,omitempty"`
}

func main() {
    rawXmlData := "NicRaboy
San FranciscoCA
MariaRaboy
" var data Data xml.Unmarshal([]byte(rawXmlData), &data) jsonData, _ := json.Marshal(data) fmt.Println(string(jsonData)) }