Created
December 25, 2013 17:56
-
-
Save ewalk153/8125351 to your computer and use it in GitHub Desktop.
go example for using XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/xml" | |
| "fmt" | |
| "log" | |
| ) | |
| type Reading struct { | |
| RType string `xml:"r_type,attr"` | |
| ReadingString string `xml:",innerxml"` | |
| } | |
| type Box struct { | |
| Reading Reading `xml:"Reading"` | |
| Raw string `xml:",innerxml"` | |
| } | |
| func main() { | |
| var box Box | |
| Xml := []byte(`<box><Reading r_type="we"><stuff>cool</stuff></Reading></box>`) | |
| if err := xml.Unmarshal(Xml, &box); err != nil { | |
| log.Fatalln("Failed", err) | |
| } | |
| fmt.Printf("Box (xml): %s\n", string(Xml)) | |
| fmt.Printf("Box: %#v\n", box) | |
| data, _ := xml.Marshal(Box{Reading: Reading{RType: "we", ReadingString: "<dude>wow</dude>"}, Raw: "<hello>cat</hello>"}) | |
| fmt.Printf("Box (xml): %s\n", string(data)) | |
| data, _ = xml.Marshal(box) | |
| fmt.Printf("Box (xml): %s\n", string(data)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment