go - 在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)
请帮忙
解决方案
使用 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"`
}
推荐阅读
- php - Mailchimp API - 查询以获取所有草稿活动
- php - Docker-compose(CakePHP 2x 和 MySQL)抛出 PHP 致命错误
- wcf - WCF服务创建多个内部对象?
- php - 在没有简单 HTML DOM 解析器的情况下从 php 页面回显图像
- android-studio - 如何避免编译器警告从未使用参数“x”
- android-fragments - 根据数据状态切换到不同的片段
- tensorflow - 提交 ml-engine 训练作业时云存储上传失败
- php - PHP - 从 2 个字符串中获取唯一值
- android - sqlite 中的 strftim() 给出错误的结果
- ios - 表格视图和标签栏