dongzheng3113 2017-01-04 00:51
浏览 46
已采纳

去XML解组数组

I am trying to unmarshal a file that looks like this

<?xml version="1.0" encoding="UTF-8"?>
<houses>
  <house name="Rhyves Flats 14" houseid="1" entryx="167" entryy="361" entryz="6" rent="0" townid="2" size="17" />
</houses>

With the following code

// House struct used for houses xml file
type House struct {
    XMLName xml.Name `xml:"houses"`
    HouseID uint32 `xml:"houseid,attr"`
    Name    string `xml:"name,attr"`
    EntryX  uint16 `xml:"entryx,attr"`
    EntryY  uint16 `xml:"entryy,attr"`
    EntryZ  uint16 `xml:"entryz,attr"`
    Size    int    `xml:"size,attr"`
    TownID  uint32 `xml:"townid,attr"`
    Rent    int    `xml:"rent,attr"`
}

// LoadHouses parses the server map houses
func LoadHouses(file string, list []House) error {
    // Load houses file
    f, err := ioutil.ReadFile(file)

    if err != nil {
        return err
    }

    // Unmarshal houses file
    return xml.Unmarshal(f, &list)
}

This is not returning any error. But the house slice is empty. Everything seems correct, the attrs are set and the XMLName too.

  • 写回答

1条回答 默认 最新

  • doudi1978 2017-01-04 01:14
    关注

    Your code is missing a definition of the Houses part of the XML. Something like what is shown below and unmarshal on that.

    type Houses struct {
        House    []House `xml:"house"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向