获取xml节点属性

时间:2018-03-04 14:26:38

标签: xml go

我正在尝试解组以下XML:

    <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.3R9/junos">
        <route-engine-information xmlns="http://xml.juniper.net/junos/12.3R9/junos-chassis">
            <route-engine>
                [...]
                <temperature junos:celsius="36">36 degrees C / 96 degrees F</temperature>
                [...]
            </route-engine>
        </route-engine-information> 
       [...]
    </rpc-reply>

最后,我想要36属性中的junos:celsius,但无法找到方法,这是我的方法:

type RoutingEngines struct {
    RoutingEngine []struct {
        [...]
        Temperature int `xml:"temperature,junos:celsius,attr"`
    } `xml:"route-engine-information>route-engine"`
}

可悲的是,这不起作用

1 个答案:

答案 0 :(得分:0)

您可以稍微更改一下结构。

type RoutingEngines struct {
    RoutingEngine []struct {
        Temperature struct {
            Celsius int    `xml:"celsius,attr"`
            Text    string `xml:",chardata"`
        } `xml:"temperature"`
    } `xml:"route-engine-information>route-engine"`
}

https://play.golang.org/p/zxXsfW-Kzsa