gpt4 book ai didi

xml - 解码转义的 XML

转载 作者:IT王子 更新时间:2023-10-29 00:54:29 31 4
gpt4 key购买 nike

在 Go 中,我将如何解码这个 XML 响应?我已经尝试在我的 Answer 结构上构建一个自定义的 UnMarshal 方法,但我的运气不太好。

<?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>

最佳答案

像这样解码两次(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)
}

关于xml - 解码转义的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23585914/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com