Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshalling escaped XML

Tags:

xml

go

In Go, how would I go about decoding this XML response? I've tried building a custom UnMarshal method on my Answer struct, but I'm not having much luck.

<?xml version="1.0"?>
<GetAssignmentResponse>
    <Answer>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
        &lt;QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd"&gt;
        &lt;Answer&gt;
        &lt;QuestionIdentifier&gt;Q1HasEvents&lt;/QuestionIdentifier&gt;
        &lt;FreeText&gt;no&lt;/FreeText&gt;
        &lt;/Answer&gt;
        &lt;/QuestionFormAnswers&gt;
    </Answer>
</GetAssignmentResponse>
like image 749
hcliff Avatar asked Mar 20 '23 15:03

hcliff


2 Answers

Decode it twice like this (try on playground)

package main

import (
    "encoding/xml"
    "fmt"
)

var data = `<?xml version="1.0"?>
<GetAssignmentResponse>
    <Answer>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
        &lt;QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd"&gt;
        &lt;Answer&gt;
        &lt;QuestionIdentifier&gt;Q1HasEvents&lt;/QuestionIdentifier&gt;
        &lt;FreeText&gt;no&lt;/FreeText&gt;
        &lt;/Answer&gt;
        &lt;/QuestionFormAnswers&gt;
    </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)
}
like image 139
Nick Craig-Wood Avatar answered Apr 20 '23 02:04

Nick Craig-Wood


As an alternative, this also works, but it's not as elegant

func (l *AssignmentAnswer) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    var (
        content string
        resp QuestionFormAnswers
    )
    if err := d.DecodeElement(&content, &start); err != nil {
        return err
    }
    c := strings.NewReader(html.UnescapeString(content))

    dec := xml.NewDecoder(c)
    if err := dec.Decode(&resp); err != nil{
        return err
    }

    *l = AssignmentAnswer{resp}
    return nil
}

type QuestionFormAnswers struct{
    Answers []Answer `xml:"Answer"`
}

type AssignmentAnswer struct{
    QuestionFormAnswers QuestionFormAnswers
}

type Assignment struct{
    AssignmentId     string
    WorkerId         string
    HITId            string
    AssignmentStatus string
    AutoApprovalTime string
    AcceptTime       string
    SubmitTime       string
    ApprovalTime     string
    Answers          AssignmentAnswer `xml:"Answer"`
}
like image 35
hcliff Avatar answered Apr 20 '23 02:04

hcliff