首页 > 解决方案 > 在golang中读取和解析xml文件但打印时没有错误和“0”值

问题描述

我想读取 XML 并使用它在程序中创建逻辑。目前,我是新手,我能够从 XML 文件创建结构,但不知何故不确定如何打印值。后来我想在程序中使用这个值作为变量。也请分享技巧来做到这一点。

下面是我正在使用的 XML 文件 -

 <?xml version="1.0" encoding="UTF-8"?>
<rules>
<rule type="rule0">
<conditions>
   <priority>1</priority>
   <tgppmccmnc>52503</tgppmccmnc>
   <rgid>300</rgid>
   <serviceclass>null</serviceclass>
   <counterstatus0>dgu</counterstatus0>
   <counterstatus1>null</counterstatus1>
  </conditions>
  <apply>
  <chargingrulename>up_lw_normal</chargingrulename>
  <chargingrulebasename>up_lw_normal</chargingrulebasename>
   <qosinfo>
     <max_bitrate_dl>500000</max_bitrate_dl>
     <max_bitrate_ul>3000000</max_bitrate_ul>
     <qci>9</qci>
     </qosinfo>
   </apply>
    </rule>
  <rule type="rule1">
   <conditions>
   <priority>1</priority>
   <tgppmccmnc>52503</tgppmccmnc>
   <rgid>300</rgid>
   <serviceclass>null</serviceclass>
   <counterstatus0>dgu</counterstatus0>
   <counterstatus1>null</counterstatus1>
    </conditions>
   <apply>
   <chargingrulename>up_lw_normal</chargingrulename>
  <chargingrulebasename>up_lw_normal</chargingrulebasename>
   <qosinfo>
     <max_bitrate_dl>500000</max_bitrate_dl>
     <max_bitrate_ul>3000000</max_bitrate_ul>
     <qci>9</qci>
   </qosinfo>
    </apply>
   </rule>
   </rules>

我创建了这些结构:

package main

import (
    "encoding/xml"
)

// our struct which contains the complete array of all rules in the file
type Rules struct {
    XMLName xml.Name `xml:"rules"`
    Rules   []Rule   `xml:"rule"`
}

// the rule struct for all policy rules
type Rule struct {
    XMLName    xml.Name   `xml:"rule"`
    Type       string     `xml:"type,attr"`
    Conditions Conditions `xml:"conditions"`
    Apply      Apply      `xml:"Apply"`
    // Name string `xml:"name"`
}

// a simple struct which contains all our
// conditions
type Conditions struct {
    XMLName        xml.Name `xml:"conditions"`
    Priority       uint32   `xml:"priority"`
    Tgppmccmnc     string   `xml:"tgppmccmnc"`
    Rgid           string   `xml:"rgid"`
    Serviceclass   string   `xml:"serviceclass"`
    Counterstatus0 string   `xml:"counterstatus0"`
    Counterstatus1 string   `xml:"counterstatus1"`
}

//Apply struct //It is used for applying speed and rule to Gx response
type Apply struct {
    XMLName              xml.Name `xml:"apply"`
    Chargingrulename     string   `xml:"chargingrulename"`
    Chargingrulebasename string   `xml:"chargingrulebasename"`
    Qosinfo              Qosinfo  `xml:"qosinfo"`
}

// Apply struct for QoS speed
type Qosinfo struct {
    XMLName        xml.Name `xml:"qosinfo"`
    Max_bitrate_dl string   `xml:"max_bitrate_dl"`
    Max_bitrate_ul string   `xml:"max_bitrate_ul"`
    Qci            string   `xml:"qci"`
}

主要的

xmlFile, err := os.Open("PolicyRules.xml")
// if we os.Open returns an error then handle it
if err != nil {
    fmt.Println(err)
}
fmt.Println("Successfully Opened the file")
// 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)
// we initialize our array
var rules Rules
var rule Rule
var conditions Conditions
xml.Unmarshal(byteValue, &rules)
xml.Unmarshal(byteValue, &rule)
xml.Unmarshal(byteValue, &conditions)
fmt.Println(rule.Conditions.Priority)

请帮忙

标签: gostruct

解决方案


使用 err 值xml.Unmarshal来调试问题:

err = xml.Unmarshal(byteValue, &rules)

这是错误:

xml:main.Rule.Apply 标记中的名称“Apply”与 main.Apply.XMLName 中的名称“apply”冲突

修改Rule 结构以修复它:

// the rule struct for all policy rules
type Rule struct {
    XMLName    xml.Name   `xml:"rule"`
    Type       string     `xml:"type,attr"`
    Conditions Conditions `xml:"conditions"`
    Apply      Apply      `xml:"apply"`
    // Name string `xml:"name"`
}

推荐阅读