0
点赞
收藏
分享

微信扫一扫

Go语言解析Xml,当标签内有属性并且有文本值时应该这样定义结构体

点亮自己的那盏灯 2022-04-24 阅读 40
gogolangxml

前言

       对不起大家哈,我已经很久没有写博客记录了,并不是没时间呀,主要是下班了就想好好休息…

回归正题

一、问题描述


 <Device id="d1" name="Mazak" uuid="M7011L016ZN">
      <Description manufacturer="Mazak Corporation" serialNumber="M7011L016ZN">Mill w/SMART</Description>
 </Device>

type MTConnectDevicesDevice struct {
	XMLName xml.Name `xml:"Device"`    //当前标签名称
	Id      string   `xml:"id,attr"`   //当前标签名称的属性id
	Name    string   `xml:"name,attr"` //当前标签名称的属性name
	Uuid    string   `xml:"uuid,attr"` //当前标签名称的属性uuid
	Description MTConnectDevicesDescription `xml:"Description"` //当前子标签名称Description
}

type MTConnectDevicesDescription struct {
	XMLName      xml.Name `xml:"Description"`         //当前标签名称
	Manufacturer string   `xml:"manufacturer,attr"`   //当前标签名称的属性 manufacturer 
	SerialNumber string   `xml:"serialNumber,attr"`   //当前标签名称的属性 serialNumber
}

二、解决问题


type MTConnectDevicesDescription struct {
	XMLName      xml.Name `xml:"Description"`         //当前标签名称
	XMLValue     string   `xml:",innerxml"`           //取当前节点的文本值
	Manufacturer string   `xml:"manufacturer,attr"`   //当前标签名称的属性 manufacturer 
	SerialNumber string   `xml:"serialNumber,attr"`   //当前标签名称的属性 serialNumber
}

三、致谢

enmm…这里说一下哈,前面有博客留下联系方式,我给重新编辑删除了,因为我消息爆炸了哈哈哈,不好意思,如果找我有事呢,可以私信我,有时间我一定回复的哈。谢谢大家,我撤了。

举报

相关推荐

0 条评论