Skip to content

Instantly share code, notes, and snippets.

@yanatan16
Created June 12, 2013 13:47
Show Gist options
  • Save yanatan16/5765409 to your computer and use it in GitHub Desktop.
Save yanatan16/5765409 to your computer and use it in GitHub Desktop.
XML usage in Go
package main
import (
"fmt"
"encoding/xml"
"github.com/clbanning/x2j"
"github.com/jmoiron/jsonq"
)
type Example struct {
Text string `xml:"hello>text"`
Punctuation string `xml:"hello>punctuation"`
}
func main() {
input := []byte("<hello><text>world</text><punctuation>!</punctuation></hello>")
ex := new(Example)
fmt.Println(xml.Unmarshal(input, ex))
ex2, err := x2j.DocToMap(string(input))
fmt.Println(err)
jq := jsonq.NewQuery(ex2)
if hello, ok := ex2["hello"].(map[string]interface{}); ok {
text, ok := ex2["text"].(string); ok {
return text
}
}
return nil, "Not found"
fmt.Println(jq.String("hello", "text"))
fmt.Println("Hello, playground")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment