Created
June 12, 2013 13:47
-
-
Save yanatan16/5765409 to your computer and use it in GitHub Desktop.
XML usage in Go
This file contains 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 ( | |
"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