在Go中解组XML元素的异构列表 [英] Unmarshalling heterogeneous list of XML elements in Go

查看:48
本文介绍了在Go中解组XML元素的异构列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的XML文档:

I have an XML document like this:

<val>
<alpha β='γ'/>
<α δ='ε'/>
(a whole bunch of the above, in random order)
</val>

换句话说,是异构列表.我想使用Go encoding/xml 包将其解组.我该怎么办?

in other words, a heterogeneous list. I would like to unmarshal it using the Go encoding/xml package. How can I do this?

推荐答案

您无法在Go中建模此类XML文档,也无法解组到 interace {} ,所以我建议事件驱动对此进行解析.

You can't model such XML documents in Go, and you can't unmarshal into interace{}, so I would suggest event-driven parsing for this.

此事件驱动的解析意味着在解析XML文档的令牌时,您会收到诸如遇到开始元素"或遇到结束元素"(当然还有元素的详细信息)之类的事件,并且事件控制程序的流程(分支和/或基于它们的内部状态更改).

This event-driven parsing means when you are parsing (the tokens of) the XML document, you receive events like "start element encountered", or "end element encountered" (with details of the element of course), and these events control the flow of your program (branch and/or change internal state based on them).

下面的示例将向您展示此原理.它不处理更复杂的XML,因为我希望示例简短,但是可以使用此技术来解析任何XML文档.

The following example will show you the principle of this. It does not handle more complex XMLs because I wanted the example to be short, but this technique can be used to parse any XML documents.

使用 xml.NewDecoder() ,并解析通过调用 Decoder.Token() >重复(循环).

Create an xml.Decoder using xml.NewDecoder(), and parse the content of the XML by calling Decoder.Token() repeatedly (in a loop).

< val> 中的元素将被收集在类型为 [] Entry 的切片中:

The elements inside <val> will be collected in a slice of type []Entry:

type Entry struct {
    Name  string
    Attr  string
    Value string
}

func main() {
    decoder := xml.NewDecoder(strings.NewReader(src))

    entries := []Entry{}
    for {
        t, err := decoder.Token()
        if err != nil {
            if err != io.EOF {
                fmt.Println(err)
            }
            break
        }
        if se, ok := t.(xml.StartElement); ok && len(se.Attr) > 0 {
            entries = append(entries, Entry{
                Name:  se.Name.Local,
                Attr:  se.Attr[0].Name.Local,
                Value: se.Attr[0].Value,
            })
        }
    }

    fmt.Printf("%+v\n", entries)
}

const src = `<val>
<alpha β='γ'/>
<α δ='ε'/>
<x y='z'/>
</val>`

输出(在游乐场上尝试):

[{Name:alpha Attr:β Value:γ} {Name:α Attr:δ Value:ε} {Name:x Attr:y Value:z}]

这篇关于在Go中解组XML元素的异构列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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