猿问

如何将嵌套的 XML 元素解组为数组?

我的 XML 包含一组预定义元素,但我无法获取该数组。这是 XML 结构:


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。


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

}

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


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

}


慕勒3428872
浏览 234回答 2
2回答

慕姐4208626

type Parent struct {&nbsp; &nbsp; Val string&nbsp; &nbsp; Children []Child&nbsp; `xml:"Children>Child"`&nbsp; //Just use the '>'}

慕侠2389804

对于这种嵌套,您还需要一个Children元素结构:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "encoding/xml")func main() {&nbsp; &nbsp; container := Parent{}&nbsp; &nbsp; err := xml.Unmarshal([]byte(xml_data), &container)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(container)&nbsp;&nbsp;&nbsp; &nbsp; }}var xml_data = `<Parent>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Val>Hello</Val>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Children>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Child><Val>Hello</Val></Child>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Child><Val>Hello</Val></Child>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Child><Val>Hello</Val></Child>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Children>&nbsp; &nbsp; &nbsp; &nbsp; </Parent>`type Parent struct {&nbsp; &nbsp; Val string&nbsp; &nbsp; Children Children}type Children struct {&nbsp; &nbsp; Child []Child}type Child struct {&nbsp; &nbsp; Val string}也贴在这里:去游乐场请注意,您的代码可以使用这种 XML 结构(在将变量名从 更改Children为 之后Child):<Parent>&nbsp; &nbsp; <Val>Hello</Val>&nbsp; &nbsp; <Child><Val>Hello</Val></Child>&nbsp; &nbsp; <Child><Val>Hello</Val></Child>&nbsp; &nbsp; <Child><Val>Hello</Val></Child></Parent>
随时随地看视频慕课网APP

相关分类

Go
我要回答