英文:
parsing xml text in golang
问题
我还没有运行代码,但是我可以帮你翻译你的代码和问题。以下是翻译好的代码:
package main
import (
"encoding/xml"
"fmt"
"log"
)
var data = []byte(`
<ports><extraports state="filtered" count="65526">
<extrareasons reason="no-responses" count="65526"/>
</extraports>
<port protocol="tcp" portid="1"><state state="closed" reason="conn-refused" reason_ttl="0"/><service name="tcpmux" method="table" conf="3"/></port>
<port protocol="tcp" portid="2"><state state="closed" reason="conn-refused" reason_ttl="0"/><service name="compressnet" method="table" conf="3"/></port>
<port protocol="tcp" portid="8"><state state="closed" reason="conn-refused" reason_ttl="0"/><service name="unknown" method="table" conf="3"/></port>
<port protocol="tcp" portid="2709"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="7748"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="12946"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="53094"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="58137"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="63139"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
</ports>
`)
type PortData struct {
Protocol string `xml:"protocol,attr"`
Port uint32 `xml:"portid,attr"`
}
type Nmap struct {
IgnoreMe xml.Name `xml:"ports"`
Data []PortData `xml:"port"`
}
func main() {
var m Nmap
if err := xml.Unmarshal(data, &m); err != nil {
log.Fatalln(err)
}
log.Println("Succeeded")
fmt.Printf("%#v\n", m)
}
你可以尝试使用上面的代码来解析你的 XML 文本。希望对你有帮助!如果你有任何其他问题,请随时问我。
英文:
Haven't had any luck parsing this snippet of xml text from the output of nmap
. I'm interested in parsing the protocol
, portid
and reason
fields out of the xml text. Initially, I tried extracting only the first two fields:
var data = []byte(`
<ports><extraports state="filtered" count="65526">
<extrareasons reason="no-responses" count="65526"/>
</extraports>
<port protocol="tcp" portid="1"><state state="closed" reason="conn-refused" reason_ttl="0"/><service name="tcpmux" method="table" conf="3"/></port>
<port protocol="tcp" portid="2"><state state="closed" reason="conn-refused" reason_ttl="0"/><service name="compressnet" method="table" conf="3"/></port>
<port protocol="tcp" portid="8"><state state="closed" reason="conn-refused" reason_ttl="0"/><service name="unknown" method="table" conf="3"/></port>
<port protocol="tcp" portid="2709"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="7748"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="12946"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="53094"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="58137"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
<port protocol="tcp" portid="63139"><state state="closed" reason="conn-refused" reason_ttl="0"/></port>
</ports>
`)
I defined my structures as below:
type PortData struct {
Protocol string `xml:"protocol,attr"`
Port uint32 `xml:"portid,attr"`
}
type Nmap struct {
IgnoreMe xml.Name `xml:"ports"`
Data []PortData `xml:"port"`
}
func main() {
var m Nmap
if err := xml.Unmarshal(data, &m); err == nil {
log.Fatalln(err)
}
log.Println("Succeeded")
fmt.Printf("%#v\n", m)
}
How can I fix my struct definitions to extract the fields of interest? Thanks
答案1
得分: 2
你可以通过更新你的结构体来提取reason
字段:
type State struct {
Reason string `xml:"reason,attr"`
}
type PortData struct {
Protocol string `xml:"protocol,attr"`
Port uint32 `xml:"portid,attr"`
State State `xml:"state"`
}
不幸的是,你不能将reason
属性解码为PortData
字段。虽然该包支持选择嵌套元素,但它不允许将其与属性选择结合使用(即xml:"state>reason,attr"
将导致Unmarshal
出错)。
其次,你的测试程序中有一个小错误:当err
为nil
时,你应该退出,而不是相反。通过这两个更改,你应该能够提取你想要的数据。
你可以在这里进行实验:http://play.golang.org/p/hVxLzZ03cC
英文:
You can extract the reason
field by updating your structs to read:
type State struct {
Reason string `xml:"reason,attr"`
}
type PortData struct {
Protocol string `xml:"protocol,attr"`
Port uint32 `xml:"portid,attr"`
State State `xml:"state"`
}
Unfortunately you can't decode the reason attribute into a PortData
field. While the package does support picking nested elements, it won't let you combine that with attribute selection (i.e. xml:"state>reason,attr"
will result in an error from Unmarshal
).
Secondly, there is a small error in your test program: you are bailing out when err
is nil, rather than the reverse. With those two changes you should be able to extract the data you're after.
You can experiment with this at: http://play.golang.org/p/hVxLzZ03cC
答案2
得分: 1
你只是打错了一个字。应该是err != nil
。
英文:
You just have a typo. It should be err != nil
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论