英文:
go xml parsing "doesn't see" any fields
问题
我一定是漏掉了一些非常明显的东西...我试图解析一个简单的XML文件,按照这里的ExampleUnmarshal()
的示例进行操作:http://golang.org/src/pkg/encoding/xml/example_test.go
正如你在下面看到的,无论是Marshal还是Unmarshal,都没有映射任何属性或子元素。据我所知,这几乎与上面的example_test.go中的操作完全相同(我能看到的唯一区别是该测试中的类型在函数范围内 - 我尝试过了,没有任何区别,并且它们使用的是子元素而不是属性 - 除了id
- 但根据doc的说明,name,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) // 输出:{{ DICTIONARY} []}
dict.ignore = "test"
out, err := xml.MarshalIndent(&dict, " ", " ")
fmt.Println(string(out)) // 输出: <DICTIONARY></DICTIONARY>
// 嗯?
}
英文:
I must be missing something really obvious... Am trying to parse a simple XML file, following the example from ExampleUnmarshal()
in here: http://golang.org/src/pkg/encoding/xml/example_test.go
As you'll see at the bottom of this, none of the attributes or child elements are being mapped - either direction - Marshal or Unmarshal. From what I can tell this is almost the exact same thing they are doing in example_test.go above (the only differences I can see are the that types in that test are within the scope of the function - which I tried, makes no diff, and they are using child elements and not attributes - except for id
- but per <a href="http://golang.org/pkg/encoding/xml/">doc</a> name,attr should work afaict).
Code looks like this:
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?
}
答案1
得分: 7
你需要将结构体字段导出(首字母大写)。
根据encoding/xml
Marshall
函数的文档:
结构体的XML元素包含结构体的每个导出字段的编组元素。
请参考https://stackoverflow.com/questions/17209111/unable-to-parse-a-complex-json-in-golang/17209311#17209311获取相关答案。
英文:
You need to Export (Capitalize) your struct fields.
From the encoding/xml
Marshall
function docs:
>>The XML element for a struct contains marshalled elements for each of the exported fields of the struct
See https://stackoverflow.com/questions/17209111/unable-to-parse-a-complex-json-in-golang/17209311#17209311 for a related answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论