doushan6692 2013-10-17 14:36
浏览 101
已采纳

Golang XML封送两个相同的属性

I'm forced to work with some poorly designed XML, I'm trying to read this XML into a Go structure. Here's some sample data:

<?xml version="1.0" encoding="UTF-8"?>
<dictionary>

    <direction from="lojban" to="English">
        <valsi word="cipni" type="gismo">
            <rafsi>cpi</rafsi>
            <definition>x1 is a bird of species x2</definition>
            <notes></notes>
        </valsi>
        ...
    </direction>

    <direction from="English" to="lojban">
        <nlword word="eagle" valsi="atkuila" />
        <nlword word="hawk" sense="bird" valsi="aksiptrina" />
        ...
    </direction>

</dictionary>

My issue is I can either read in nodes or because they both contain the attribute "word":

main.NLWord field "Word" with tag "word,attr" conflicts with field "Valsi" with tag "word,attr"

I'm starting to think unmarshalling may be the wrong approach as I'd ideally structure the data differently. Should I read the XML in using some other method and build up data structures manually?

type Valsi struct {
    Word  string   `xml:"word,attr"`
    Type  string   `xml:"type,attr"`
    Def   string   `xml:"definition"`
    Notes string   `xml:"notes"`
    Class string   `xml:"selmaho"`
    Rafsi []string `xml:"rafsi"`
}

//Whoever made this XML structure needs to be painfully taught a lesson...
type Collection struct {
    From  string  `xml:"from"`
    To    string  `xml:"to"`
    Valsi []Valsi `xml:"valsi"`
}

type Vlaste struct {
    Direction []Collection `xml:"direction"`
}

var httpc = &http.Client{}

func parseXML(data []byte) Vlaste {
    vlaste := Vlaste{}
    err := xml.Unmarshal(data, &vlaste)
    if err != nil {
        fmt.Println("Problem Decoding!")
        log.Fatal(err)
    }
    return vlaste
}

展开全部

  • 写回答

1条回答 默认 最新

  • dsajkdadsa14222 2013-10-17 15:43
    关注

    I may not have grasped what your problem is but the following structure works fine for me (your modified example on play):

    type Valsi struct {
        Word  string   `xml:"word,attr"`
        Type  string   `xml:"type,attr"`
        Def   string   `xml:"definition"`
        Notes string   `xml:"notes"`
        Class string   `xml:"selmaho"`
        Rafsi []string `xml:"rafsi"`
    }
    
    type NLWord struct {
        Word  string   `xml:"word,attr"`
        Sense string   `xml:"sense,attr"`
        Valsi string   `xml:"valsi,attr"`
    }
    
    type Collection struct {
        From  string    `xml:"from,attr"`
        To    string    `xml:"to,attr"`
        Valsi []Valsi   `xml:"valsi"`
        NLWord []NLWord `xml:"nlword"`
    }
    
    type Vlaste struct {
        Direction []Collection `xml:"direction"`
    }
    

    In this structure, Collection can have Valsi values as well as NLWord values. You can then decide, depending on From/To or the length of either Valsi or NLWord how to handle the data.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

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

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

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

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

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

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

客服 返回
顶部