gpt4 book ai didi

xml - 在 Go 中解析 xml

转载 作者:IT王子 更新时间:2023-10-29 00:44:26 28 4
gpt4 key购买 nike

我想解析像示例中那样结构的 XML 位。我一直遇到空值。这是我正在使用的简化版本,只是为了说明问题。

package main

import (
"encoding/xml"
"fmt"
)

type Entry struct {
VulnCveId string `xml:"entry>vuln:cve-id"`
}

func main() {
v := Entry{}
err := xml.Unmarshal([]byte(data), &v)
if err != nil {
fmt.Printf("error: %v", err)
return
}

fmt.Println(v.VulnCveId)
}

const data = `
<entry id="CVE-2005-4895">
<vuln:vulnerable-configuration id="http://nvd.nist.gov/">
<cpe-lang:logical-test negate="false" operator="OR">
<cpe-lang:fact-ref name="cpe:/a:csilvers:gperftools:0.3" />
<cpe-lang:fact-ref name="cpe:/a:csilvers:gperftools:0.2" />
<cpe-lang:fact-ref name="cpe:/a:csilvers:gperftools:0.1" />
</cpe-lang:logical-test>
</vuln:vulnerable-configuration>
<vuln:vulnerable-software-list>
<vuln:product>cpe:/a:csilvers:gperftools:0.3</vuln:product>
<vuln:product>cpe:/a:csilvers:gperftools:0.1</vuln:product>
<vuln:product>cpe:/a:csilvers:gperftools:0.2</vuln:product>
</vuln:vulnerable-software-list>
<vuln:cve-id>CVE-2005-4895</vuln:cve-id>
<vuln:published-datetime>2012-07-25T15:55:01.273-04:00</vuln:published-datetime>
<vuln:last-modified-datetime>2012-08-09T00:00:00.000-04:00</vuln:last-modified-datetime>
<vuln:cvss>
<cvss:base_metrics>
<cvss:score>5.0</cvss:score>
<cvss:access-vector>NETWORK</cvss:access-vector>
<cvss:access-complexity>LOW</cvss:access-complexity>
<cvss:authentication>NONE</cvss:authentication>
<cvss:confidentiality-impact>NONE</cvss:confidentiality-impact>
<cvss:integrity-impact>NONE</cvss:integrity-impact>
<cvss:availability-impact>PARTIAL</cvss:availability-impact>
<cvss:source>http://nvd.nist.gov</cvss:source>
<cvss:generated-on-datetime>2012-07-26T08:38:00.000-04:00</cvss:generated-on-datetime>
</cvss:base_metrics>
</vuln:cvss>
<vuln:cwe id="CWE-189" />
<vuln:references xml:lang="en" reference_type="UNKNOWN">
<vuln:source>MISC</vuln:source>
<vuln:reference href="http://kqueue.org/blog/2012/03/05/memory-allocator-security-revisited/" xml:lang="en">http://kqueue.org/blog/2012/03/05/memory-allocator-security-revisited/</vuln:reference>
</vuln:references>
<vuln:references xml:lang="en" reference_type="UNKNOWN">
<vuln:source>CONFIRM</vuln:source>
<vuln:reference href="http://code.google.com/p/gperftools/source/browse/tags/perftools-0.4/ChangeLog" xml:lang="en">http://code.google.com/p/gperftools/source/browse/tags/perftools-0.4/ChangeLog</vuln:reference>
</vuln:references>
<vuln:summary>Multiple integer overflows in TCMalloc (tcmalloc.cc) in gperftools before 0.4 make it easier for context-dependent attackers to perform memory-related attacks such as buffer overflows via a large size value, which causes less memory to be allocated than expected.</vuln:summary>
</entry>
`

v.VulnCveId 在此实例中为空。我做错了什么?

最佳答案

问题是您没有 namespace 。你有一个前缀“vuln”,但它没有在任何地方声明。它实际上什至不是有效的 XML。

将第一行设为:

<entry xmlns:vuln="http://my-namespace.com" id="CVE-2005-4895">

然后让你的结构标签成为这个

`xml:"entry>http://my-namespace.com cve-id"`

你应该可以开始了。

关于xml - 在 Go 中解析 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12011622/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com