xml
golang的xml处理主要应用Unmarshal、Marshal方法实现,解析一个xml到struct如下,首先是xml文件:
<?xml version="1.0" encoding="utf-8"?>
<servers version="1.0">
<!--测试注释server-->
<server desc="s1">
<serverName>Shanghai_VPN</serverName>
<serverIP>127.0.0.1</serverIP>
</server>
<server desc="s2">
<!---->
<serverName>Beijing_VPN</serverName>
<serverIP>127.0.0.2</serverIP>
</server>
</servers>
下面记录下主要代码:
// parse2xml project main.go
package main import ( "encoding/xml"
"fmt"
"io/ioutil"
"os" ) type Recurlyservers struct { XMLName xml.Name `xml:"servers"` Version string `xml:"version,attr"` Svs []server `xml:"server"` Description string `xml:",innerxml"` } type server struct { XMLName xml.Name `xml:"server"` Desc string `xml:"desc,attr"` ServerName string `xml:"serverName"` ServerIP string `xml:"serverIP"` TestDesc string `xml:",innerxml"` } func main() { file, err := os.Open("servers.xml") if err != nil { fmt.Printf("%s", err.Error()) panic(err) } defer file.Close() data, err := ioutil.ReadAll(file) v := Recurlyservers{} err = xml.Unmarshal(data, &v) if err != nil { fmt.Printf("%s", err.Error()) panic(err) } fmt.Printf("%#v", v) }
打印结果:
可以发现Unmarshal解析时的一些规则:
1、解析使用struct的tag配置,通过底层反射实现
2、类型为xml.Name的struct字段XMLName对应的是xml中的“父”节点名称,如servers
3、*,attr对应的为当前父节点上的属性名称,如version,attr,对应的便是xml文件中servers节点的version属性
4、tag中仅写一个名称如serverName,表示serverName为节点名称
5、对于某个struct中,innerxml的tag标注,该struct的属性对应的数据为该struct相应的XMLName对应的xml下的所有内容,比如TestDesc对应的为server节点下的所有内容,Description对应的便是servers下的所有内容
由struct生成对应的xml,方法如下:
// struct2xmll project main.go
package main import ( "encoding/xml"
"fmt"
"os" ) type Servers struct { XMLName xml.Name `xml:"servers"` Version string `xml:"version,attr"` Svs []server `xml:"server"` } type server struct { ServerName string `xml:"serverName"` ServerIP string `xml:"serverIP"` } func main() { v := &Servers{Version: "1.0"} v.Svs = append(v.Svs, server{"Shanghai_VPN", "127.0.0.1"}) v.Svs = append(v.Svs, server{"Beijing_VPN", "127.0.0.1"}) output, err := xml.MarshalIndent(v, " ", " ") if err != nil { fmt.Println(err.Error()) return } os.Stdout.Write([]byte(xml.Header)) file, err := os.Create("servers.xml") file.Write([]byte(xml.Header)) file.Write(output) }
生成的xml文档:
json
将json字符串解析为struct:
// jsonparse project main.go
package main import ( "encoding/json"
"fmt" ) type Server struct { ServerName string `json:"serverName"` ServerIP string `json:"serverIP"` } type Serversslice struct { Servers []Server `json:"servers"` } func main() { var s Serversslice str := `{"servers":[{"serverName":"Shanghai_VPN","serverIP":"127.0.0.1"},{"serverName":"Beijing_VPN","serverIP":"192.168.20.132"}]}` json.Unmarshal([]byte(str), &s) fmt.Printf("%#v", s) }
结果:
将一个struct解析为json如下:
package main import ( "encoding/json"
"fmt" ) type Server struct { serverName string `json:"serverName"` ServerIP string `json:"serverIP"` } type ServersSlice struct { Servers []Server `json:"servers"` Desc string `json:"desc"` } func main() { servers := []Server{{serverName: "admin", ServerIP: "192.168.20.131"}, {serverName: "admin1", ServerIP: "192.168.20.132"}} desc := "描述部分"
var ss ServersSlice ss.Desc = desc ss.Servers = servers js, _ := json.Marshal(ss) fmt.Printf("Json: %s", js) }
结果:
有疑问加站长微信联系(非本文作者)