golang xml解析不确定是否存在的元素
2017-08-29 15:41
726 查看
golang中负责解析函数
func Unmarshal(data []byte, v interface{}) error
只能对struct, slice和string进行解析
golang中负责生成xml函数
func Marshal(v interface{}) ([]byte, error)marshal可以处理指针指向的值, 若指针为nil, 则不会写入到xml中.
我们可以在定义解析的struct结构时, 将元素的类型定义为指针类型, 若解析时不存在该元素, 则在marshal时不会生成空的元素.
type ObjectInfo struct {
// attr
ObjectType string `xml:"ObjectType,attr"`
Id string `xml:"Id,attr"`
Name string `xml:"Name,attr"`
CreationDate string //`xml:"CreationDate"` // struct tag is not necessary
// tag
Tag []TagInfo `xml:"TagCollection>Tag"`
// ColorValues
RefSpecData *ReflectanceSpectrumInfo `xml:"ColorValues>ReflectanceSpectrum"`
ColorLabData *ColorCIELabInfo `xml:"ColorValues>ColorCIELab"`
ColorDensityData *ColorDensityInfo `xml:"ColorValues>ColorDensity"`
//ColorValues PrintDataInterface `xml:",innerxml"`
// DeviceColorValues
ColorCMYKPlusNData *ColorCMYKPlusNInfo `xml:"DeviceColorValues>ColorCMYKPlusN"`
ColorCMYKData *ColorCMYKInfo `xml:"DeviceColorValues>ColorCMYK"`
//DeviceColorValues PrintDataInterface `xml:",innerxml"`
}上面结构是在解析CxF定义的结构, 扫描后生成的数据类型是不确定的, 但又是固定的几组结构, 指针类型能很好的解决这个问题.
func Unmarshal(data []byte, v interface{}) error
只能对struct, slice和string进行解析
golang中负责生成xml函数
func Marshal(v interface{}) ([]byte, error)marshal可以处理指针指向的值, 若指针为nil, 则不会写入到xml中.
我们可以在定义解析的struct结构时, 将元素的类型定义为指针类型, 若解析时不存在该元素, 则在marshal时不会生成空的元素.
type ObjectInfo struct {
// attr
ObjectType string `xml:"ObjectType,attr"`
Id string `xml:"Id,attr"`
Name string `xml:"Name,attr"`
CreationDate string //`xml:"CreationDate"` // struct tag is not necessary
// tag
Tag []TagInfo `xml:"TagCollection>Tag"`
// ColorValues
RefSpecData *ReflectanceSpectrumInfo `xml:"ColorValues>ReflectanceSpectrum"`
ColorLabData *ColorCIELabInfo `xml:"ColorValues>ColorCIELab"`
ColorDensityData *ColorDensityInfo `xml:"ColorValues>ColorDensity"`
//ColorValues PrintDataInterface `xml:",innerxml"`
// DeviceColorValues
ColorCMYKPlusNData *ColorCMYKPlusNInfo `xml:"DeviceColorValues>ColorCMYKPlusN"`
ColorCMYKData *ColorCMYKInfo `xml:"DeviceColorValues>ColorCMYK"`
//DeviceColorValues PrintDataInterface `xml:",innerxml"`
}上面结构是在解析CxF定义的结构, 扫描后生成的数据类型是不确定的, 但又是固定的几组结构, 指针类型能很好的解决这个问题.
相关文章推荐
- Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式
- 在config配置文件添加iis的Mime类型,检测文件中是否存在添加语句,使用xml解析方法
- Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式
- warning MSB3245: 未能解析此引用。未能找到程序集“CemeteryBLL”。请检查磁盘上是否存在该程序集。 如果您的代码需要此引用,则可能出现编译错误。
- 判断XML里是否存在某个节点
- jQuery -- 判断元素是否存在
- XMLHTTP.Open Head 检测网络链接是否存在..
- 判断一个int数组中的元素是否存在重复
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- android检查sqlite数据库中是否存在某个元素
- 用js获取对象之前首先检测元素是否存在
- 使用jquery怎么去检查一个元素是否存在
- 在JSP使用EL和JSTL判断指定元素是否存在于指定集合中
- C# 判断数组中是否存在该元素
- indexOf() 如何判断一个元素在指定数组中是否存在? 找出指定元素出现的所有位置? indexOf()方法 是正序查找,lastIndexOf()是倒叙查找
- 数组是否存在重复元素
- 解析xml,判断是否含有某字符串
- SQL:判断某些元素是否存在及创建的SQL语句
- 判断控件数组中的元素是否存在
- js 判断一个元素是否存在