英文:
Extracting xml attributes with go
问题
如何从像<vuln:cwe id="CWE-189" />这样的元素中提取出'id'属性?这是嵌套在<entry>下面的。我的初始尝试是类似于xml:"entry>cwe,id,attr"。
英文:
How can I extract the 'id' attribute out of an element like <vuln:cwe id="CWE-189" />? This is nested under <entry>. My initial attempt was something like xml:"entry>cwe,id,attr".
答案1
得分: 2
两种方法:
1/ 一直获取Token(),直到元素的名称为cwe。然后可以使用结构体"CweXml"提取id属性:
type CweXml struct {
Id string `xml:"id,attr"`
}
2/ 或者从entry开始,但在这种情况下,您需要一个包含CweXml结构体的结构体。
type EntryXml struct {
Cwe CweXml `xml:"cwe"`
}
在这种情况下,您可以在entryXml.Cwe.Id中找到id。
英文:
Two approaches:
1/ keep getting the Token(), until the element has for name cwe.
Then a struct "CweXml" can extract the id attribute:
type CweXml struct {
Id string `xml:"id,attr"`
}
2/ Or start from entry, but in that case you need a struct for it, which will contain CweXml struct.
type EntryXml struct {
Cwe CweXml `xml:"cwe"`
}
In that case, you will find id in your entryXml.Cwe.Id.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论