第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

Go
富國滬深 2021-11-15 16:12:18
我想xml使用golang. 作為使用 的新手go,我閱讀了網(wǎng)絡(luò)上的文章,解釋了如何解析 XML,但我不確定為什么我的返回值nil在這種情況下。package mainimport (    "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 回答

?
撒科打諢

TA貢獻1934條經(jīng)驗 獲得超2個贊

我將調(diào)試反饋放在評論中,但我剛剛修改了您的示例以使其正常工作,可以在此處進行測試;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)

}

這是我所做的四項更改的概要;


1)打印Books對象而不是從返回的錯誤Unmarshal


2)大寫字段的第一個字母,Book使它們“導(dǎo)出”,以便其他包(在這種情況下為解組器)可以獲取/設(shè)置它們


3)添加xml屬性。在導(dǎo)出字段時,它使它沒有隱式字符串匹配,因此您必須明確指定將哪個 xml 值讀入每個字段


4)BookList為此更新 XML 路徑,您說它將是 Books>Book 但這意味著您的 xml 中不存在另一個嵌套級別。這個對象是Books,您想要在該列表中的元素將具有簡單的相對 xpath,Book這就是您放置在那里的內(nèi)容。


查看完整回答
反對 回復(fù) 2021-11-15
  • 1 回答
  • 0 關(guān)注
  • 280 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號