dsqdpn31467 2016-06-04 11:55
浏览 15
已采纳

去编组XML

I currently have the following XML

<monster name="Valkyrie" nameDescription="a valkyrie" race="blood" experience="85" speed="190" manacost="450">
    <health now="190" max="190" />
    <look type="139" head="113" body="57" legs="95" feet="113" corpse="20523" />
    <voices interval="5000" chance="10">
        <voice sentence="Another head for me!" />
        <voice sentence="Head off!" />
        <voice sentence="Your head will be mine!" />
        <voice sentence="Stand still!" />
        <voice sentence="One more head for me!" />
    </voices>
</monster>

And I am reading it using the following structs

type monster struct {
    XMLName xml.Name `xml:"monster"`
    Name string `xml:"name,attr"`
    NameDescription string `xml:"nameDescription,attr"`
    Race string `xml:"race,attr"`
    Experience int `xml:"experience,attr"`
    Speed int `xml:"speed,attr"`
    ManaCost int `xml:"manacost,attr"`
    Health monsterHealth `xml:"health"`
    Look monsterLook `xml:"look"`
    Voices monsterVoice `xml:"voices"`
}

type monsterVoice struct {
    Voices []monsterSentence
}

type monsterSentence struct {
    XMLName xml.Name `xml:"voice"`
    Sentence string `xml:"sentence,attr"`
}

type monsterLook struct {
    Type int `xml:"type,attr"`
    Head int `xml:"head,attr"`
    Body int `xml:"body,attr"`
    Legs int `xml:"legs,attr"`
    Feet int `xml:"feet,attr"`
    Corpse int `xml:"corpse,attr"`
}

type monsterHealth struct {
    Now int `xml:"now,attr"`
    Max int `xml:"max,attr"`
}

But I am not sure how to read the voices element

  • 写回答

2条回答 默认 最新

  • dql7588 2016-06-04 13:01
    关注

    You just missed to specify XML element mapping for Voices :

    type monsterVoice struct {
        Voices []monsterSentence `xml:"voice"`
    }
    

    after that small addition, unmarshalling as usual should work :

    var result monster
    err := xml.Unmarshal([]byte(your_xml_data_string), &result)
    
    if err != nil {
        fmt.Println(err)
    }
    for _, r := range result.Voices.Voices {
        fmt.Println(r.Sentence)
    }
    

    <kbd>playground demo 1</kbd>

    better yet, drop monsterVoice and use child selector like so :

    type monster struct {
        XMLName xml.Name `xml:"monster"`
        ....
        Voices []monsterSentence `xml:"voices>voice"`
    }
    

    Then we can get rid of the awkward result.Voices.Voices in the previous demo :

    for _, r := range result.Voices {
        fmt.Println(r.Sentence)
    }
    

    <kbd>playground demo 2</kbd>

    output : (both demo yields the same output)

    Another head for me!
    Head off!
    Your head will be mine!
    Stand still!
    One more head for me!
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集