嵌套 XML/JSON 结构标签,定义结构的正确方法是什么?

读取请求正文时,很少有 XML 标签未被解码


我已经使用 json 和 xml 标签定义了嵌套结构,因为我想在 json 和 xml 中对请求和响应使用相同的架构。


var dataNewTestplans DataTestplan

err := xml.NewDecoder(r.Body).Decode(&dataNewTestplans)

xmlData, _ := xml.Marshal(dataNewTestplans)

fmt.Printf(string(xmlData))

数据测试计划结构:


type DataTestplan struct {

    Data []Testplan `json:"data" xml:"data"`

}

测试计划结构:


type Testplan struct {

    Group      string      `json:"group" xml:"group"`

    Name       string      `json:"name" xml:"name"`

    Parameters []Parameter `json:"parameters,omitempty" xml:"parameters,omitempty"`

    Released   bool        `json:"released" xml:"released"`

    Teststeps  []Teststep  `json:"teststeps,omitempty" xml:"teststeps,omitempty"`

    Version    int32       `json:"version" xml:"version"`

}

参数结构:


type Parameter struct {

    XMLName     xml.Name `xml:"parameters"`

    Comment     string   `json:"comment" xml:"comment"`

    Description string   `json:"description,omitempty" xml:"description,omitempty"`

}

测试步骤结构:


type Teststep struct {

    XMLName xml.Name `xml:"teststeps"`

    AslID   string   `json:"aslId,omitempty" xml:"aslID"`

    Bin     int32    `json:"bin,omitempty" xml:"bin"`

}

XML 发送:


<root>

  <data>

    <element>

     <group>TEST</group>

     <name>TEST</name>

     <parameters>

        <element>

           <comment>test</comment>

           <description>test</description>

        </element>

        <element>

           <comment>test1</comment>

        </element>

     </parameters>

     <released>true</released>

     <teststeps>

        <element>

           <bin>32</bin>

        </element>

      </teststeps>

     <version>1</version>

    </element>

  </data>

</root>

XML 解码:


<DataTestplan>

  <data>

    <group></group>

    <name></name>

    <released>false</released>

    <version>0</version>

    </data>

</DataTestplan>

我知道缺少标签很可能是因为结构定义中缺少 xml 标记,但我不确定为什么解码的信息缺少标签值?XML 和 JSON 编码有什么技巧?


跃然一笑
浏览 117回答 2
2回答

慕娘9325324

您可以>在标签中使用。如果 XML 元素包含名称与格式为“a”或“a>b>c”的标记前缀匹配的子元素,unmarshal 将深入到 XML 结构中查找具有给定名称的元素,并将映射该结构体字段的最里面的元素。以 ">" 开头的标记相当于以字段名称开头,后跟 ">" 的标记。如果字段使用标签“a>b>c”,则元素 c 将嵌套在父元素 a 和 b 内。命名相同父级的彼此相邻的字段将包含在一个 XML 元素中。例如:type DataTestplan struct {     Data []Testplan `json:"data" xml:"data>element"` }

MM们

无需为每个标签创建不同的结构。您可以将它们全部嵌入其中。对于 xml 解析,您缺少一些标签。>您可以通过在 xml 标记中使用 来避免一些代码复制。当您迭代两个元素时,这将有效地“合并”它们。下面是一个更简洁的实现示例:type Root struct {&nbsp; &nbsp; XMLName xml.Name `xml:"root"`&nbsp; &nbsp; Data&nbsp; &nbsp; []struct {&nbsp; &nbsp; &nbsp; &nbsp; Group&nbsp; &nbsp; &nbsp; string `json:"group" xml:"group"`&nbsp; &nbsp; &nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp;string `json:"name" xml:"name"`&nbsp; &nbsp; &nbsp; &nbsp; Parameters []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Comment&nbsp; &nbsp; &nbsp;string `json:"comment" xml:"comment,omitempty"`&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description string `json:"description" xml:"description,omitempty"`&nbsp; &nbsp; &nbsp; &nbsp; } `json:"parameters" xml:"parameters>element"`&nbsp; &nbsp; &nbsp; &nbsp; Released&nbsp; bool `json:"released" xml:"released"`&nbsp; &nbsp; &nbsp; &nbsp; Teststeps []struct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bin int32 `json:"bin" xml:"bin"`&nbsp; &nbsp; &nbsp; &nbsp; } `xml:"teststeps>element,omitempty"`&nbsp; &nbsp; &nbsp; &nbsp; Version int32 `json:"version" xml:"version"`&nbsp; &nbsp; } `json:"data" xml:"data>element"`}通过运行:var dataNewTestplans DataTestplanerr := xml.NewDecoder(r.Body).Decode(&dataNewTestplans)xmlData, _ := xml.Marshal(dataNewTestplans)fmt.Printf(string(xmlData))你得到:<root><data><element><group>TEST</group><name>TEST</name><parameters><element><comment>test</comment><description>test</description></element><element><comment>test1</comment></element></parameters><released>true</released><teststeps><element><bin>32</bin></element></teststeps><version>1</version></element></data></root>
打开App,查看更多内容
随时随地看视频慕课网APP