如何以切片格式解析XML

问题描述 投票:0回答:1

我有这个XML:

   <?xml version="1.0" encoding="ASCII"?><QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd"><Answer><QuestionIdentifier>annotatedResult.boundingBoxes</QuestionIdentifier><FreeText>[{"height":641,"label":"F-22 Raptor","left":82,"top":97,"width":1088}]</FreeText></Answer><Answer><QuestionIdentifier>annotatedResult.inputImageProperties.height</QuestionIdentifier><FreeText>839</FreeText></Answer><Answer><QuestionIdentifier>annotatedResult.inputImageProperties.width</QuestionIdentifier><FreeText>1260</FreeText></Answer></QuestionFormAnswers>

我不知道如何解析以获取[{"height":641,"label":"F-22 Raptor","left":82,"top":97,"width":1088}]并获取诸如heightweight等单个值。最后获得641, 82, 971088

我根据发现的指南进行了尝试:

type DimensionInfo struct {

    Answer  struct {
    FreeText []string `xml:"freetext"`
    } `xml:"answer"`
}


var data = []byte(`<?xml version="1.0" encoding="ASCII"?><QuestionFormAnswers xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd"><Answer><QuestionIdentifier>annotatedResult.boundingBoxes</QuestionIdentifier><FreeText>[{"height":641,"label":"F-22 Raptor","left":82,"top":97,"width":1088}]</FreeText></Answer><Answer><QuestionIdentifier>annotatedResult.inputImageProperties.height</QuestionIdentifier><FreeText>839</FreeText></Answer><Answer><QuestionIdentifier>annotatedResult.inputImageProperties.width</QuestionIdentifier><FreeText>1260</FreeText></Answer></QuestionFormAnswers>`) 

var t DimensionInfo
xml.Unmarshal(data, &t)
fmt.Println(t.Answer.FreeText)

我正在获取空的切片/列表

xml go unmarshalling
1个回答
0
投票

错误xml.Unmarshal返回将被忽略,但是该功能在指定的输入数据流上失败,因为所谓的XML声明(<?xml ... ?>位)声明该数据流是使用不同于UTF-8的编码进行编码的-该标准指定的唯一有效编码¹,因此解码器只是拒绝继续。

© www.soinside.com 2019 - 2024. All rights reserved.