drzrzzkh462254 2018-12-15 20:26
浏览 127
已采纳

在嵌套的XML Golang中解组属性

I've been searching everywhere and can't figure out the solution to this.

I'm trying to get an attribute of a nested xml tag from wikipedia, but have so far been unable to get the nested attribute. I'm specifically trying to get pageid from the page tag.

Here's an example of the xml:

<api batchcomplete="">
  <query>
    <pages>
      <page _idx="25039021" pageid="25039021" ns="0" title="Go (programming language)">
        <extract xml:space="preserve">
          stuff about golang
        </extract>
      </page>
    </pages>
  </query>
</api>

Here are the structs that I'm using:

type Page struct {
    PageID string `xml:"pageid,attr"`
}

type Extract struct {
    Text string `xml:"query>pages>page>extract"`
    Page Page   `xml:"query>pages>page"`
}

but still extract.page.pageid comes back with nothing. Anything I'm missing/doing wrong?

  • 写回答

1条回答 默认 最新

  • dongxian5735 2018-12-15 20:37
    关注

    Your structs should change to this :

    type Page struct {
        PageID string `xml:"pageid,attr"`
        Text   string `xml:"extract"`
    }
    
    type Extract struct {
        Page Page `xml:"query>pages>page"`
    }
    
    fmt.Println(extract.Page.PageID)
    fmt.Println(extract.Page.Text)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料