dongri1989 2014-04-17 06:33
浏览 388
已采纳

Golang XML属性和值

I can't seem to figure out why this isn't working

 type HostProperties struct {                                                                                                                     
          XMLName xml.Name   `xml:HostProperties"`                                                                                                 
          Info    []InfoList `xml:"tag"`                                                                                                           
 }                                                                                                                                                
  type InfoList struct {                                                                                                                           
          HostEnd         string  `xml:",chardata"`                                                                                                
          PatchSummary    string  `xml:",chardata"`                                                                                                
          CPE1            string  `xml:",chardata"`                                                                                                
          CPE0            string  `xml:",chardata"`                                                                                                
          SystemType      string  `xml:",chardata"`                                                                                                
          OperatingSystem string  `xml:",chardata"`                                                                                                
          MacAddress      string  `xml:",chardata"`                                                                                                
          Traceroute      string  `xml:",chardata"`                                                                                                
          IP              string  `xml:",chardata"`                                                                                                
          FQDN            string  `xml:",chardata"`                                                                                                
          HostStart       string  `xml:",chardata"`                                                                                                
  }
<HostProperties>
<tag name="HOST_END">Thu Feb 20 12:38:24 2014</tag>
<tag name="patch-summary-total-cves">4</tag>
<tag name="cpe-1">cpe:/a:openbsd:openssh:5.6 -&gt; OpenBSD OpenSSH 5.6</tag>
<tag name="cpe-0">cpe:/o:vmware:esx_server</tag>
<tag name="system-type">hypervisor</tag>
<tag name="operating-system">VMware ESXi</tag>
<tag name="mac-address">00:00:00:00:00:00</tag>
<tag name="traceroute-hop-0">172.28.28.29</tag>
<tag name="host-ip">172.28.28.29</tag>
<tag name="host-fqdn">foobar.com</tag>
<tag name="HOST_START">Thu Feb 20 12:30:14 2014</tag>
</HostProperties>

Results

{HostEnd:172.28.28.29 PatchSummary: CPE1: CPE0: SystemType: OperatingSystem: MacAddress: Traceroute: IP: FQDN: HostStart:}

It creates a bunch of new slices with only the first element filled in and even then it's the wrong element. It's not filling out the other variables. The rest of the file seems to parse fine, just can't figure out this part.

  • 写回答

2条回答 默认 最新

  • dpoppu4300 2014-04-17 07:42
    关注

    I don't think you can make the XML parsing work like that. Here is the best I could come up with (run it on the playground)

    var data = `<HostProperties>
    <tag name="HOST_END">Thu Feb 20 12:38:24 2014</tag>
    <tag name="patch-summary-total-cves">4</tag>
    <tag name="cpe-1">cpe:/a:openbsd:openssh:5.6 -&gt; OpenBSD OpenSSH 5.6</tag>
    <tag name="cpe-0">cpe:/o:vmware:esx_server</tag>
    <tag name="system-type">hypervisor</tag>
    <tag name="operating-system">VMware ESXi</tag>
    <tag name="mac-address">00:00:00:00:00:00</tag>
    <tag name="traceroute-hop-0">172.28.28.29</tag>
    <tag name="host-ip">172.28.28.29</tag>
    <tag name="host-fqdn">foobar.com</tag>
    <tag name="HOST_START">Thu Feb 20 12:30:14 2014</tag>
    </HostProperties>`
    
    type HostProperties struct {
        XMLName xml.Name `xml:HostProperties"`
        Tags    []Tag    `xml:"tag"`
    }
    
    type Tag struct {
        Key   string `xml:"name,attr"`
        Value string `xml:",chardata"`
    }
    
    func main() {
        v := new(HostProperties)
        err := xml.Unmarshal([]byte(data), v)
        if err != nil {
            fmt.Printf("error: %v", err)
            return
        }
        fmt.Printf("v = %#v
    ", v)
    
    }
    

    If you really want that InfoList structure you'll have to run through the Tags and fill it in. I'd probably just stick it in a map[string]string though like this

    tags := make(map[string]string)
    for _, tag := range v.Tags {
        tags[tag.Key] = tag.Value
    }
    fmt.Printf("map = %#v
    ", tags)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?