This is a creation in Article, where the information may have evolved or changed.
Xml
Golang XML processing mainly applies unmarshal, marshal method implementation, parsing an XML to a struct as follows, the first is an XML file:
<?XML version= "1.0" encoding= "Utf-8"?><Serversversion= "1.0"> <!--Test Comment Server - <Serverdesc= "S1"> <ServerName>Shanghai_vpn</ServerName> <ServerIP>127.0.0.1</ServerIP> </Server> <Serverdesc= "S2"> <!----> <ServerName>Beijing_vpn</ServerName> <ServerIP>127.0.0.2</ServerIP> </Server></Servers>
The following records the main code:
//Parse2xml Project Main.go PackageMainImport ( "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") ifErr! =Nil {fmt. Printf ('%s ', Err. Error ()) Panic (ERR)} defer file. Close () data, err:=Ioutil. ReadAll (file) V:=recurlyservers{} Err= XML. Unmarshal (Data, &v)ifErr! =Nil {fmt. Printf ('%s ', Err. Error ()) Panic (ERR)} FMT. Printf ("% #v", v)}
Printing results:
You can find some rules for Unmarshal parsing:
1, the analysis uses the struct's tag configuration, realizes through the bottom reflection
2, the type is XML. The struct field for name XMLName corresponds to the "parent" node name in the XML, such as servers
3. *,attr corresponds to the name of the property on the current parent node, such as Version,attr, which corresponds to the version property of the Servers node in the XML file
4, the tag only write a name such as ServerName, indicating that servername is the node name
5, for a struct, the tag label of InnerXml, the property of the struct corresponds to the corresponding xmlname of the struct, all content under the corresponding XML, such as Testdesc corresponding to the server node of all the content, Description corresponds to all the content under servers.
The corresponding XML is generated by the struct, by the following method:
//STRUCT2XMLL Project Main.go PackageMainImport ( "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, "", "") ifErr! =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)}
The resulting XML document:
Json
To parse a JSON string into a struct:
//Jsonparse Project Main.goPackage main Import ("Encoding/json" "FMT") Type Serverstruct{ServerNamestring' JSON:"ServerName"' ServerIPstring' JSON:"ServerIP"'} type Serversslicestruct{Servers []server ' JSON:"Servers"'} func main () {vars 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)}
Results:
Parse a struct into JSON as follows:
Package main Import ("Encoding/json" "FMT") Type Serverstruct{serverNamestring' JSON:"ServerName"' ServerIPstring' JSON:"ServerIP"'} type Serversslicestruct{Servers []server ' JSON:"Servers"' Descstring' JSON:"desc"'} func main () {servers:= []server{{servername:"Admin", ServerIP:"192.168.20.131"}, {serverName:"admin1", ServerIP:"192.168.20.132"}} desc:="Description Section" varSS Serversslice SS. Desc=Desc SS. Servers=Servers js, _:=JSON. Marshal (ss) fmt. Printf ("Json:%s", JS)}
Results: