Created
November 7, 2012 19:30
-
-
Save RadioactiveMouse/4033829 to your computer and use it in GitHub Desktop.
XML parsing in Go
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 ( | |
"fmt" | |
"encoding/xml" | |
"net/http" | |
"errors" | |
) | |
type Answer struct { | |
correct boolean | |
answer string | |
} | |
type Question struct { | |
question string | |
ask string | |
answerOne Answer | |
answerTwo Answer | |
answerThree Answer | |
answerFour Answer | |
} | |
func parseXML(input io.ReadWriter) ([]Question,error) { | |
output, err := xml.NewDecoder(Question).Decode(input) | |
if err != nil { | |
return nil, err | |
} | |
return output, nil | |
} | |
func main() { | |
res, err := http.Get("http://www.macs.hw.ac.uk/~pjbk/quiz/history.xml") | |
if err != nil { | |
log.Fatal("Error during GET request.") | |
} | |
res, er := parseXML(res) | |
if er != nil { | |
log.Fatal("Error during XML parsing") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment