当前位置:网站首页>golang xml 处理动态属性
golang xml 处理动态属性
2022-08-09 06:21:00 【ggaofeng】
golang处理读写xml时,是和一个结构体对应的。
因此,每个每个xml节点的属性,都是结构体的字段,是定死的。
type Student struct{
Name string
Address Addr
}
type Addr struct {
City string `xml:"city"`
Build string `xml:"build,attr"`
}
<Student>
<Name>gaofeng</Name>
<Address build="2b">
<city>sz</city>
</Address>
</Student>
但是如果有些属性是动态,比如
待解析/待生成的数据可能是数据1,也可能是数据2. 就是说MyItem有哪些属性,不是提前定死的。
数据1:
<Student>
<Name>gaofeng</Name>
<Address build="2b">
<city>sz</city>
<MyItem 临时1="vvv&11" 临时2="vvv22">
</MyItem>
</Address>
</Student>
数据2:
<Student>
<Name>gaofeng</Name>
<Address build="2b">
<city>sz</city>
<MyItem 临时4="vvv22">
</MyItem>
</Address>
</Student>可以使用下面的代码进行处理
package main
import (
"encoding/xml"
"fmt"
)
type Student struct{
Name string
Address Addr
}
type Addr struct {
City string `xml:"city"`
Build string `xml:"build,attr"`
MyItem Item
}
type Item struct {
Attributes []xml.Attr `xml:",any,attr"`
}
func main() {
p := &Student{Name:"gaofeng", Address:Addr{City:"sz",Build:"2b"}}
p.Address.MyItem = Item{}
p.Address.MyItem.Attributes = []xml.Attr{}
tt:=xml.Attr{Name: xml.Name{ Space:"", Local :"临时1" }, Value:"vvv&11"}
p.Address.MyItem.Attributes = append(p.Address.MyItem.Attributes, tt)
tt=xml.Attr{Name: xml.Name{ Space:"", Local :"临时2" }, Value:"vvv22"}
p.Address.MyItem.Attributes = append(p.Address.MyItem.Attributes, tt)
// 生成xml
buf, _ := xml.Marshal(p)
fmt.Println(string(buf))
// 解析xml
var s = `<Student><Name>gaofeng</Name><Address build="2b"><city>sz</city><MyItem 临时1="vvv&11" 临时2="vvv22"></MyItem></Address></Student>`
fmt.Println(s)
pp := new(Student)
xml.Unmarshal([]byte(s), pp)
fmt.Println(pp.Address.City)
fmt.Println(pp.Address.MyItem.Attributes[0].Value)
}
边栏推荐
- 手把手教你用C语言制作七夕流星雨---优雅永不过时(详细教程)
- S7-200SMART PLC Modbus TCP通信
- GNNExplainer applied to node classification task
- 牛客每日刷题之链表
- MYSQLg高级------批量插入百万级数据量
- Remember a nest.js route that matches all the path problems that follow
- 程序性能分析 —— 复杂度分析
- Text String Length Sorting - Online Tool
- 半胱氨酸/半乳糖/苝二酰亚胺功能化Fe3O4四氧化三铁纳米材料|科研试剂
- How to find package information and pin definitions for NXP S32K1xx series microcontrollers
猜你喜欢

phpstudy 安装 flarum论坛

聚酰胺-胺(PAMAM)树形聚合物-硫化铋复合纳米粒子|硫化铋修饰Gd‑DTPA‑OA配体|科研实验用

【Word】Add subscripts to the text of Word at the same time

IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用

MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
![[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands](/img/20/a0fb44e9360837146d0ed696c9e992.png)
[MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂

DevNet: Deviation Aware Networkfor Lane Detection

运算放大器(OPA)超详细参数讲解-运放---以及8个型号的运算放大器分析对比

中英文说明书丨TRC 交替醇(Catalogue NumberA575760)
随机推荐
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
[mysql database] the use of mysql database
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
Initials-Letter Query Tool-Word Abbreviation Query Online Tool
获取开发版安全码SHA1时遇到的报错
中英文说明书丨TRC D-阿卓糖(D-Altrose)
数据中台项目前期总结
Introduction to AIOT
Chapter 3 Search and Graph Theory (1)
Qt 学习(三) —— Qt 模块
Online tool for sorting multi-line strings
正则表达式-判断字符串是否匹配“AABB”模式
缓存技术使用
redis 运行lua 脚本 出现Invalid argument(s)
【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
Regular Expression - Determine if a string matches the "AABB" pattern
Data center project preliminary summary
【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
untiy 倒计时