dqlk31541 2017-03-19 15:44
浏览 17
已采纳

将XML元素名称解组为其他属性

I am currently writing a library for the NameSilo API. I am stuck on the getPriceList api, which returns XML like this:

<namesilo>
    <request>
        <operation>getPrices</operation>
        <ip>55.555.55.55</ip>
    </request>
    <reply>
        <code>300</code>
        <detail>success</detail>
        <com>
            <registration>8.99</registration>
            <transfer>8.39</transfer>
            <renew>8.99</renew>
        </com>
        <net>
            <registration>9.29</registration>
            <transfer>8.99</transfer>
            <renew>9.29</renew>
        </net>
    </reply>
</namesilo>

As you can see, there is an element for each TLD. I would like to unmarshal the element name (example: com, net) into a property which is not called XMLName (I want it to be called TLD).

After reading lines 34-39 of https://golang.org/src/encoding/xml/marshal.go, it seems like this is not possible.

I have tried the following code, but it does not work.

type APIResponse struct {
    Request struct {
        Operation string `xml:"operation"`
        IP        string `xml:"ip"`
    } `xml:"request"`
}

type GetPricesResponse struct {
    APIResponse
    Reply []struct {
        Domains []struct {
            TLD xml.name
            Registration string `xml:"registration"`
            Transfer     string `xml:"transfer"`
            Renew        string `xml:"renew"`
        } `xml:",any"`
    } `xml:"reply"`
}

Is there any way I can do this, or is it not possible to have a property name other than XMLName for the xml element name.

UPDATE: I looked a bit more into the code, and I found this, which makes me think I cannot do this easily.

展开全部

  • 写回答

2条回答 默认 最新

  • doubei5310 2017-03-20 00:28
    关注

    There's no simpler alternative to XMLName xml.Name.

    It's possible to do what you want with a type satisfying the unmarshaller interface. The added complexity is probably not worthwhile. Playground example:

    package main
    
    import (
        "encoding/xml"
        "fmt"
        "log"
    )
    
    func main() {
        var data Data
        if err := xml.Unmarshal(payload, &data); err != nil {
            log.Fatal(err)
        }
    
        for k, v := range data.Reply.Domains {
            fmt.Printf("%d: %#v
    ", k, v)
        }
    
    }
    
    type Domain struct {
        TLD          string
        Registration string `xml:"registration"`
        Transfer     string `xml:"transfer"`
        Renew        string `xml:"renew"`
    }
    
    func (domain *Domain) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
        v := struct {
            XMLName      xml.Name
            Registration string `xml:"registration"`
            Transfer     string `xml:"transfer"`
            Renew        string `xml:"renew"`
        }{}
        d.DecodeElement(&v, &start)
    
        domain.TLD = v.XMLName.Local
        domain.Registration = v.Registration
        domain.Transfer = v.Transfer
        domain.Renew = v.Renew
    
        return nil
    }
    
    type Data struct {
        Request struct {
            Operation string `xml:"operation"`
            Ip        string `xml:"ip"`
        } `xml:"request"`
        Reply struct {
            Code    string   `xml:"code"`
            Detail  string   `xml:"detail"`
            Domains []Domain `xml:",any"`
        } `xml:"reply"`
    }
    
    var payload = []byte(`<namesilo>
        <request>
            <operation>getPrices</operation>
            <ip>55.555.55.55</ip>
        </request>
        <reply>
            <code>300</code>
            <detail>success</detail>
            <com>
                <registration>8.99</registration>
                <transfer>8.39</transfer>
                <renew>8.99</renew>
            </com>
            <net>
                <registration>9.29</registration>
                <transfer>8.99</transfer>
                <renew>9.29</renew>
            </net>
        </reply>
    </namesilo>`)
    

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部