duannei1477 2018-09-21 10:56
浏览 23
已采纳

如何进行XML解析?

I am trying to parse an XML file and I am new to Go. I have the file below and I want to store the name and value of config tag as a key value pair and I am stuck.

The XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <TestFramework>
        <config>
            <name>TEST_COMPONENT</name>
            <value>STORAGE</value>
            <description>
           Name of the test component.
           </description>
        </config>
        <config>
            <name>TEST_SUIT</name>
            <value>STORAGEVOLUME</value>
            <description>
           Name of the test suit.
            </description>
        </config>
    </TestFramework>
 </root>

This what I have tried:

package main

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

type StructFramework struct{
    Configs []Config `"xml:config"`
}
type Config struct{
    Name string
    Value string
}
func main(){
    xmlFile, err := os.Open("config.xml")   
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Successfully Opened config.xml")
// defer the closing of our xmlFile so that we can parse it later on
    defer xmlFile.Close()
// read our opened xmlFile as a byte array.
    byteValue, _ := ioutil.ReadAll(xmlFile)
    var q StructFramework
    xml.Unmarshal(byteValue, &q)
    fmt.Println(q.Config.Name)
}
  • 写回答

1条回答 默认 最新

  • doubi5127 2018-09-21 11:13
    关注

    You need to improve your xml struct tags, it's kind a tricky for newcomers how to parse xml, here is an example:

    package main
    
    import (
        "encoding/xml"
        "fmt"
    )
    
    type StructFramework struct {
        Configs []Config `xml:"TestFramework>config"`
    }
    type Config struct {
        Name  string `xml:"name"`
        Value string `xml:"value"`
    }
    
    func main() {
        xmlFile := `<?xml version="1.0" encoding="UTF-8"?>
    <root>
        <TestFramework>
            <config>
                <name>TEST_COMPONENT</name>
                <value>STORAGE</value>
                <description>
               Name of the test component.
               </description>
            </config>
            <config>
                <name>TEST_SUIT</name>
                <value>STORAGEVOLUME</value>
                <description>
               Name of the test suit.
                </description>
            </config>
        </TestFramework>
     </root>`
        var q StructFramework
        xml.Unmarshal([]byte(xmlFile), &q)
        fmt.Printf("%+v", q)
    }
    

    Playground

    Output:

    => {Configs:[{Name:TEST_COMPONENT Value:STORAGE} {Name:TEST_SUIT Value:STORAGEVOLUME}]}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图