如何将嵌套的XML元素解组到数组中? [英] How do I unmarshal nested XML elements into an array?

查看:53
本文介绍了如何将嵌套的XML元素解组到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的XML包含一个预定义元素的数组,但是我无法选择该数组.这是XML结构:

My XML contains an array of predefined elements, but I can't pick up the array. Here is the XML structure:

var xml_data = `<Parent>
                   <Val>Hello</Val>
                   <Children>
                      <Child><Val>Hello</Val></Child>
                      <Child><Val>Hello</Val></Child>
                      <Child><Val>Hello</Val></Child>
                   </Children>
                </Parent>`

这是完整的代码,这是游乐场链接.运行此命令将提取Parent.Val,但不会提取Parent.Children.

Here is the full code and here is the playground link. Running this will pick up Parent.Val, but not Parent.Children.

package main

import (
    "fmt"
    "encoding/xml"
)

func main() {

    container := Parent{}
    err := xml.Unmarshal([]byte(xml_data), &container)

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(container)  
    }
}

var xml_data = `<Parent>
                   <Val>Hello</Val>
                   <Children>
                      <Child><Val>Hello</Val></Child>
                      <Child><Val>Hello</Val></Child>
                      <Child><Val>Hello</Val></Child>
                   </Children>
                </Parent>`

type Parent struct {
    Val string
    Children []Child
}

type Child struct {
    Val string
}

我在此处简化了问题.基本上,我不能解组任何数组,而不仅是预定义结构.以下是更新的工作代码.在该示例中,只有一个项目最终出现在容器界面中.

I simplified the problem a bit here. Basically I can't unmarshal any array, not just of predefined structure. Below is the updated work code. In that example, only one item ends up in the container interface.

func main() {

    container := []Child{}
    err := xml.Unmarshal([]byte(xml_data), &container)

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(container)  
    }

    /* 
        ONLY ONE CHILD ITEM GETS PICKED UP
    */
}

var xml_data = `
            <Child><Val>Hello</Val></Child>
            <Child><Val>Hello</Val></Child>
            <Child><Val>Hello</Val></Child>
        `

type Child struct {
    Val string
}

推荐答案

对于这种嵌套,您还需要一个 Children 元素的结构:

For this kind of nesting, you'll need also a struct for Children element:

package main

import (
    "fmt"
    "encoding/xml"
)

func main() {

    container := Parent{}
    err := xml.Unmarshal([]byte(xml_data), &container)

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(container)  
    }
}

var xml_data = `<Parent>
            <Val>Hello</Val>
            <Children>
                <Child><Val>Hello</Val></Child>
                <Child><Val>Hello</Val></Child>
                <Child><Val>Hello</Val></Child>
            </Children>
        </Parent>`

type Parent struct {
    Val string
    Children Children
}

type Children struct {
    Child []Child
}

type Child struct {
    Val string
}

也粘贴在此处:​​去游乐场

请注意,您的代码可以使用以下XML结构运行(将变量名称从 Children 更改为 Child 后):

Note that your code would work (after changing variable name from Children to Child) with this kind of XML structure:

<Parent>
    <Val>Hello</Val>
    <Child><Val>Hello</Val></Child>
    <Child><Val>Hello</Val></Child>
    <Child><Val>Hello</Val></Child>
</Parent>

这篇关于如何将嵌套的XML元素解组到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆