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.

xml

2个回答

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)
dongshan1811
dongshan1811 谢谢你的工作。 我将不得不重新考虑如何处理整个文件。 可能必须流传输文件才能在匹配的块中获取数据。
6 年多之前 回复
dshfjsh_5455
dshfjsh_5455 我添加了一些有关如何将数据放入地图游乐场的信息
6 年多之前 回复
doushou8730
doushou8730 看起来几乎就在那里,但是它会将每个值放在自己的行上。 我喜欢map [string] string的想法。 我将如何处理? 或者我可以在哪里看。 真的很新。 {Key:HOST_END Value:Thu Feb 20 12:38:24 2014} {Key:patch-summary-total-cves Value:4}
6 年多之前 回复



标记 xml:“,chardata” </ code>将根据需要选择当前元素的字符数据,但是 仅适用于带有该标签的第一个字段。 这就是为什么您得到观察到的结果的原因。</ p>

对于给定的XML,我建议解码为以下类型:</ p>

  type  HostProperties结构{
XMLName xml.Namexml:“ HostProperties”
Info [] Tagxml:“ tag”
}

type Tag结构{
Name stringxml:“ name,attr “
值字符串xml:”,chardata“
}
</ code> </ pre>

尽管如此,它不会自动将各种命名标签拆分为单独的字段 :您需要在处理XML之后执行此操作。</ p>
</ div>

展开原文

原文

The tag xml:",chardata" will select the current element's character data, as you want, but only for the first field with that tag. This is why you got the results you observed.

For the given XML, I would suggest decoding into the following types:

type HostProperties struct {
    XMLName xml.Name `xml:"HostProperties"`
    Info    []Tag    `xml:"tag"`
}

type Tag struct {
    Name  string `xml:"name,attr"`
    Value string `xml:",chardata"`
}

It won't automatically split the various named tags into separate fields for you though: you will need to do that after processing the XML.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问