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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题