在Go中,我将如何解码此 XML响应?我已经尝试在我的Answer结构上构建一个自定义的UnMarshal方法,但我没有太多运气. ?xml version="1.0"?GetAssignmentResponse Answerlt;?xml version="1.0" encoding="UTF-8" standa
<?xml version="1.0"?> <GetAssignmentResponse> <Answer><?xml version="1.0" encoding="UTF-8" standalone="no"?> <QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd"> <Answer> <QuestionIdentifier>Q1HasEvents</QuestionIdentifier> <FreeText>no</FreeText> </Answer> </QuestionFormAnswers> </Answer> </GetAssignmentResponse>像这样解码它两次( try on playground)
package main import ( "encoding/xml" "fmt" ) var data = `<?xml version="1.0"?> <GetAssignmentResponse> <Answer><?xml version="1.0" encoding="UTF-8" standalone="no"?> <QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd"> <Answer> <QuestionIdentifier>Q1HasEvents</QuestionIdentifier> <FreeText>no</FreeText> </Answer> </QuestionFormAnswers> </Answer> </GetAssignmentResponse>` type Response struct { XMLName xml.Name `xml:"GetAssignmentResponse"` Answer string `xml:"Answer"` } type Answer struct { XMLName xml.Name `xml:"QuestionFormAnswers"` FreeText string `xml:"FreeText"` } func main() { v := Response{} err := xml.Unmarshal([]byte(data), &v) if err != nil { fmt.Printf("error: %v", err) return } fmt.Printf("Answer = %q\n", v.Answer) a := Answer{} err = xml.Unmarshal([]byte(v.Answer), &a) if err != nil { fmt.Printf("error: %v", err) return } fmt.Printf("Answer = %#v\n", a) }