2013-09-30 29 views
1

我必須失去了一些東西真的很明顯...我試圖解析一個簡單的XML文件,以下從ExampleUnmarshal()在這裏的例子:http://golang.org/src/pkg/encoding/xml/example_test.go去xml解析「沒有看到」任何領域

如您在底部看到,沒有任何屬性或子元素被映射 - 無論是方向 - 元帥還是Unmarshal。從我可以告訴的這幾乎是他們在上面的example_test.go中完全相同的事情(我能看到的唯一區別是那個測試中的類型在函數的範圍內 - 我試過了,沒有差異,他們正在使用子元素而不是屬性 - 除了id - 但是每個doc名稱,attr都應該工作)。

代碼如下所示:

package main 

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


type String struct { 
    XMLName xml.Name `xml:"STRING"` 
    lang string `xml:"lang,attr"` 
    value string `xml:"value,attr"` 
} 

type Entry struct { 
    XMLName xml.Name `xml:"ENTRY"` 
    id  string `xml:"id,attr"` 
    strings []String 
} 

type Dictionary struct { 
    XMLName xml.Name `xml:"DICTIONARY"` 
    thetype string `xml:"type,attr"` 
    ignore string `xml:"ignore,attr"` 
    entries []Entry 
} 

func main() { 

    dict := Dictionary{} 

    b := []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<DICTIONARY type="multilanguage" ignore="en"> 
    <ENTRY id="ActionText.Description.AI_ConfigureChainer"> 
    <STRING lang="en" value="ActionText.Description.AI_ConfigureChainer"/> 
    <STRING lang="da" value=""/> 
    <STRING lang="nl" value=""/> 
    <STRING lang="fi" value=""/> 
    </ENTRY> 
</DICTIONARY> 
`) 

    err := xml.Unmarshal(b, &dict) 
    if err != nil { panic(err) } 

    fmt.Println(dict) // prints: {{ DICTIONARY} []} 

    dict.ignore = "test" 

    out, err := xml.MarshalIndent(&dict, " ", " ") 
    fmt.Println(string(out)) // prints: <DICTIONARY></DICTIONARY> 

    // huh? 

} 

回答