当前位置:网站首页>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)
}
边栏推荐
- Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
- GNNExplainer applied to node classification task
- 什么是excel文件保护
- AIOT介绍
- Web APIs BOM- 操作浏览器:本地存储
- Adds, deletes, searches, and changes the leading doubly circular linked list (implemented in C language)
- Word文件的只读模式没有密码怎么退出?
- [MySQL] Second, the relationship between processes, MySQL password cracking, table building and database building related commands
- 【Feel】In the Unity Feel plugin, Camera cannot display CameraShake correctly
- How to automatically fill down an excel table without dragging the mouse down
猜你喜欢
年薪35W的测试工程师被裁亲身经验:不得不听的忠告
MYSQLg高级------批量插入百万级数据量
RNN-T
[Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?
Getting Started with MATLAB Image Processing
Introduction to AIOT
Unity Gobang Game Design and Simple AI (2)
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
[email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂"/>
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
Bismuth sulfide nanorods with CT imaging function | Bismuth sulfide-zinc protoporphyrin composites (PAMAM/Bi2S3 composite nanoparticles)
随机推荐
Qt learning (3) - Qt module
治疗消化性溃疡—Toronto Research Chemicals 甘氨酸铝
Online tool for sorting multi-line strings
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
Gao Zelong, a famous digital collection expert and founder of the Digital Collection Conference, was interviewed by China Entrepreneur Magazine
深度学习-神经网络原理2
MYSQL高级篇-----查询截取分析,锁机制,主从复制
Xilinx Zynq ZynqMP DNA
sql problem solving statement to create a table
直接用的zip包 缺少很多依赖,pip没有,感觉用anaconda create一个环境会方便点
关于如何查找NXP S32K1xx系列单片机的封装信息和引脚定义
IQ Products巨细胞病毒CMV感染检测试剂盒的特征和应用
[HNOI2002]营业额统计
Unity五子棋游戏设计 和简单AI实现(1)
Getting Started with MATLAB Image Processing
51 serial communication (on)
GNNExplainer应用于节点分类任务
5年测试开发工程师感悟——写给还在迷茫中的朋友
声母-字母查询工具-词语缩写查询在线工具