当前位置:网站首页>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)
}
边栏推荐
- 获取开发版安全码SHA1时遇到的报错
- Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
- uniapp实现防抖搜索
- 弄潮 Web3 欧易OKX全球「抢人」
- sql问题解答创建表的语句
- The 24th day of the special assault version of the sword offer
- [Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?
- ZIP压缩包文件删除密码的方法
- Web APIs BOM- 操作浏览器:本地存储
- 中英文说明书丨CalBioreagents 山羊抗人白蛋白,IgG组分
猜你喜欢
Introduction of convenient functions and convenient shortcut keys of vs tomato assistant
牛客每日刷题之链表
Getting started with kubernetes apparmor
深度学习-神经网络原理2
SiO2 / KH550 modified ferroferric oxide nano magnetic particles | PDA package the ferromagnetic oxide nanoparticles (research)
MYSQLg高级------批量插入百万级数据量
数据中台项目前期总结
harbor企业级镜像仓库搭建
Bismuth sulfide nanorods with CT imaging function | Bismuth sulfide-zinc protoporphyrin composites (PAMAM/Bi2S3 composite nanoparticles)
Likou Brush Question 180
随机推荐
【Word】Add subscripts to the text of Word at the same time
How to find package information and pin definitions for NXP S32K1xx series microcontrollers
获取开发版安全码SHA1时遇到的报错
四氧化三铁/硫化铋纳米复合材料([email protected]@BSABiS纳米颗粒)|树状大分子稳定的硫化铋纳米颗粒|科研试剂
[HNOI2002]营业额统计
什么是excel文件保护
锁执行的过程
Go lang1.18入门精炼教程——第一章:环境搭建
Text String Length Sorting - Online Tool
shell regular expressions
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
GNNExplainer应用于节点分类任务
[R language] Normalize and organize files into folders of various file types
Program Performance Analysis - Complexity Analysis
harbor企业级镜像仓库搭建
[R language] Extract all files under a folder to a specific folder
PDF不能打印和复制的问题如何解决?
Regular Expression - Determine if a string matches the "AABB" pattern
MYSQL Advanced Chapter - Query Interception Analysis, Lock Mechanism, Master-Slave Replication
Remember a nest.js route that matches all the path problems that follow