dsgw8802 2018-09-25 07:33
浏览 29
已采纳

Go中的XML解析,用于不同父标记下的相同标记

I am new to Go, and I am trying to parse an XML file. My goal is to get the values of all the config tags by making only one structure for them. Not a separate structure for each parent tag. Below is the XML file and what I have done.

My XML file:

<?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>abc@xyz.com</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>

My Go program:

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)
    }
}

I know I can make a struct like this:

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

But I don't want to use this:

`xml:"TestFramework>config"`

I am getting output like this:

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

D:\Go>
  • 写回答

1条回答 默认 最新

  • doupuchen6378 2018-09-25 07:53
    关注

    You can use the ,any tag option.

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度