2015-10-13 60 views
-1

我想用golang解析xml。作爲使用go的新手,我已經閱讀了關於Web的文章,解釋瞭如何解析XML,但我不確定爲什麼我的返回值是nil<nil>解析golang中的xml字符串

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) 
} 
+3

您正在打印錯誤,因爲它的工作原因,它是零。 'xml.Unmarshal'不返回正在傳入的值 - second arg'&b'。另外在你輸入'Book'時,這些字段是未導出的(需要在第一個字母時改爲大寫),所以解組器不能設置它們的值。 – evanmcdonnal

回答

2

我把調試反饋,評論,但是我剛剛修改了例如工作,它可以在這裏進行測試; 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)打印代替ERR從Unmarshal

2返回Books對象)大寫領域的第一個字母上Book,使他們「出口」,使他們能夠獲得/其他包設置(在解組在這種情況下)

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

4)更新BookList的XML路徑,因爲您認爲它應該是Books> Book,但這意味着你的xml中不存在另一個嵌套層次。這個對象是Books,你想要在那個列表中的元素將有一個簡單的Book的相對xpath,這就是你放在那裏的東西。