go - 解码嵌套的 OPML 文档

标签 go opml

我正在尝试解码一个简单的嵌套 opml 文档。当我嵌套了 opml 并且我无法决定结构时,问题就来了。我把代码放在下面。请告诉我如何解析 Outline 结构的多层嵌套。

http://play.golang.org/p/FobiL1JDdb

    package main

    import (
        "encoding/xml"
        "fmt"
    )

    var response = `<opml version='1.0'>
     <head>
      <title>More Cases</title>
      <expansionState>1,6,26</expansionState>
     </head>
     <body>
      <outline text='Testing' _note='indeterminate'>
       <outline text='Weekly' _status='indeterminate'>
       </outline>
       </outline>
     </body>
    </opml>`

    type Opml struct {
        XMLName xml.Name
        Version string `xml:"version,attr"`
        Head    Head   `xml:"head"`
        Body    Body   `xml:"body"`
    }

    type Head struct {
        Title          string `xml:"title"`
        ExpansionState string `xml:"expansionState"`
    }

    type Body struct {
        Outline Outline `xml:"outline"`
    }
    type Outline struct {
        Text string `xml:"text,attr"`
        Note string `xml:"_note,attr"`
    }

    func main() {

        fmt.Println("UnMrashal XML")
        opml := &Opml{}
        xml.Unmarshal([]byte(response), opml)
        fmt.Println(opml)
    }

最佳答案

你需要使用指针

type Outline struct {
    Text string `xml:"text,attr"`
    Note string `xml:"_note,attr"`
    Outline *Outline `xml:"outline"`
}

http://play.golang.org/p/f1UqEkJq4S

关于go - 解码嵌套的 OPML 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32247493/

相关文章:

arrays - 将 int[] 传递给函数但函数返回空数组

struct - 嵌入式类型和结构的多态性

pandas - 属性错误 : module 'pandas' has no attribute 'read_xml' or 'to_xml'

python - Youtube-dl 订阅 mp3

php - 关于用 PHP 解析 OPML 文件以便我可以将地址导入 mySql 数据库的任何建议?

android - 我们如何解析 OPML 字符串

sql - Goland 可以使用其他 SQL 包自动完成 SQL 语句吗?

go - beego中处理表单提交的正确方法是什么?

go - 是否可以将结构的 slice 作为接口(interface)传递给方法?

python - 如何使用正则表达式在 OPML (XML) 文件中查找带引号的属性值