doushan6692 2013-10-17 22: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 23: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.

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器