duanfei9278 2015-03-04 01:44
浏览 60
已采纳

在golang中解析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
", m)
}

How can I fix my struct definitions to extract the fields of interest? Thanks

  • 写回答

2条回答 默认 最新

  • dousuochu7291 2015-03-04 02:32
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)
  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题