dongluyi5123 2015-10-13 20:42
浏览 54
已采纳

<nil>在golang中解析xml字符串时

I would like to parse xml using golang. Being new to using go, I've read articles around the web, explaining how to parse XML but I'm not sure why my return value is nil in this case.

package main

import (
    "fmt"
    //"io/ioutil"
    "encoding/xml"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

type Books struct {
    XMLName xml.Name `xml:"Books"`
    BookList []Book `xml:"Books>Book"`
}

type Book struct {
    title string `xml:"title,attr"`
    author string
    published string
}

func main() {
    //f, err := ioutil.ReadFile("xml/Books.xml")
    //check(err)

    var data = []byte(`
        <Books>
            <Book title="A Brief History of Time" author="Stephen Hawking" published="1988">
                <title>title here</title>
                A Brief History of Time: From the Big Bang to Black Holes is a 1988 popular-science book by British physicist Stephen Hawking. It became a bestseller and sold more than 10 million copies in 20 years.
            </Book>
            <Book title="Steve Jobs" author="Walter Isaacson" published="2011">
                Steve Jobs is the authorized self-titled biography book of Steve Jobs. The book was written at the request of Jobs by Walter Isaacson, a former executive at CNN.
            </Book>
        </Books>
    `)

    b := Books{}
    o := xml.Unmarshal([]byte(data), &b)
    fmt.Println(o)
}
  • 写回答

1条回答 默认 最新

  • douya1855 2015-10-13 20:54
    关注

    I put the debugging feedback in a comment but I've just modified your example to work and it can be tested here; https://play.golang.org/p/_UIph2je7f

    package main
    
    import (
        "fmt"
        //"io/ioutil"
        "encoding/xml"
    )
    
    func check(e error) {
        if e != nil {
            panic(e)
        }
    }
    
    type Books struct {
        XMLName xml.Name `xml:"Books"`
        BookList []Book `xml:"Book"`
    }
    
    type Book struct {
        Title string `xml:"title,attr"`
        Author string `xml:"author,attr"`
        Published string `xml:"published,attr"`
    }
    
    func main() {
        //f, err := ioutil.ReadFile("xml/Books.xml")
        //check(err)
    
        var data = []byte(`
            <Books>
                <Book title="A Brief History of Time" author="Stephen Hawking" published="1988">
                    <title>title here</title>
                    A Brief History of Time: From the Big Bang to Black Holes is a 1988 popular-science book by British physicist Stephen Hawking. It became a bestseller and sold more than 10 million copies in 20 years.
                </Book>
                <Book title="Steve Jobs" author="Walter Isaacson" published="2011">
                    Steve Jobs is the authorized self-titled biography book of Steve Jobs. The book was written at the request of Jobs by Walter Isaacson, a former executive at CNN.
                </Book>
            </Books>
        `)
    
        b := Books{}
        o := xml.Unmarshal([]byte(data), &b)
        fmt.Println(o)
        fmt.Println(b)
    }
    

    Here's a rundown of the four changes I made;

    1) print the Books object instead of the err returned from Unmarshal

    2) uppercase the first letter of fields on Book to make them 'exported' so they can be get/set by other packages (the unmarshaller in this case)

    3) add xml attributes. In exporting the fields it makes it so there isn't an implicit string match so you gotta explicitly specify which xml value is read into each field

    4) update the XML path for BookList for this you said it would be Books>Book but that implies another level of nesting that didn't exist in your xml. This object is Books, the elements you want in that list would have a relative xpath of simply Book so that's what you put there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿