Go中不同父标签下相同标签的XML解析

标签 xml go struct xml-parsing

我是 Go 新手,我正在尝试解析 XML 文件。我的目标是通过只为所有配置标签创建一种结构来获取它们的值。每个父标签不是一个单独的结构。下面是 XML 文件以及我所做的事情。

我的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <TestFramework>
        <config>
            <name>TEST_COMPONENT</name>
            <value>FILMS</value>
            <description>
           Name of the test Films.
            </description>
        </config>
        <config>
            <name>TESTER_NAME</name>
            <value><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f2939091b28a8b88dc919d9f" rel="noreferrer noopener nofollow">[email protected]</a></value>
            <description>
            Name or email of the tester.
            </description>
        </config>
    </TestFramework>
    <Product>
        <config>
            <name>PRODUCT_RELEASE</name>
            <value>2.1</value>
            <description>
            Name or email of the tester.
            </description>
        </config>
        <config>
            <name>PRODUCT_BUILD</name>
            <value>7.1.3182018</value>
            <description>
            Name or email of the tester.
            </description>
        </config>
    </Product>
</root>

我的Go程序:

package main

import (
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "os"
)

type ConfigFile struct {
    Configs []Config `xml:"config"`
}

type Config struct {
    Name  string `xml:"name"`
    Value string `xml:"value"`
}

func main() {
    xmlFile, err := os.Open("octa_config.xml")
    m := make(map[string]string)
    if err != nil {
        fmt.Println(err)
    }else {
        fmt.Println("Successfully Opened octa_config.xml")
    }
    defer xmlFile.Close()
    byteValue, _ := ioutil.ReadAll(xmlFile)
    var c ConfigFile
    xml.Unmarshal(byteValue,&c)
    for i := 0; i < len(c.Configs); i++ {
        m[c.Configs[i].Name]=c.Configs[i].Value
    }
    for k,v :=range m{
        fmt.Println( k,v)
    }
}

我知道我可以制作这样的结构:

type TestFramework struct {
    Configs []Config `xml:"TestFramework>config"`
}

但我不想使用这个:

`xml:"TestFramework>config"`

我得到这样的输出:

D:\Go>go run config_nim.go
Successfully Opened octa_config.xml

D:\Go>

最佳答案

您可以使用 ,any 标记选项。

type root struct {
    XMLName xml.Name     `xml:"root"`
    Files   []ConfigFile `xml:",any"`
}

type ConfigFile struct {
    Configs []Config `xml:"config"`
}

type Config struct {
    Name        string `xml:"name"`
    Value       string `xml:"value"`
    Description string `xml:"description"`
}

https://play.golang.org/p/osaA5t5BnHi

关于Go中不同父标签下相同标签的XML解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52492893/

相关文章:

pointers - Golang 结构的指针或无指针引用其他结构及其原因

struct - 如何将函数作为结构中的成员

xml - 如何使用 Azure APIM 创建 OCSP 请求和验证响应?

java - 通过telnet和socket发送xml的问题

http - 是否可以在 Go 中将代理/ socks 与 http2 客户端一起使用?

go - 为什么 Go 编译器不将未声明的变量视为错误?

C struct 语法的说明

c - 超越比较 : compare whole function calls or xml blocks

xml - CDATA 中的 XSL 转义 HTML

Golang 模板 : what is in the context?