英文:
Parsing XML with Go, having multiple elements in lowercase
问题
我不知道如何使这段代码工作。我只是尝试解析一个简单的XML文件,像这样:
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
XMLName xml.Name `xml:"data"`
Nam string `xml:"nam,attr"`
}
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data
}
func main() {
x := `
<struct>
<data nam="MESSAGE_TYPE">
</data>
<data nam="MESSAGE_TYPE2">
</data>
</struct>
`
s := Struct{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", s)
fmt.Println(s.Data)
}
我得到的结果是:
{{ struct} []}
[]
但是当我将"data"元素改为大写时,像这样:
```go
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
XMLName xml.Name `xml:"Data"`
Nam string `xml:"nam,attr"`
}
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data
}
func main() {
x := `
<struct>
<Data nam="MESSAGE_TYPE">
</Data>
<Data nam="MESSAGE_TYPE2">
</Data>
</struct>
`
s := Struct{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", s)
fmt.Println(s.Data)
}
我得到的结果是:
{{ struct} [{{ Data} MESSAGE_TYPE} {{ Data} MESSAGE_TYPE2}]}
[{{ Data} MESSAGE_TYPE} {{ Data} MESSAGE_TYPE2}]
有人能告诉我为什么吗?
英文:
I don't know how to make this code work. I'm just trying to parse a simple XML file like this:
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
XMLName xml.Name `xml:"data"`
Nam string `xml:"nam,attr"`
}
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data
}
func main() {
x := `
<struct>
<data nam="MESSAGE_TYPE">
</data>
<data nam="MESSAGE_TYPE2">
</data>
</struct>
`
s := Struct{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", s)
fmt.Println(s.Data)
}
what I got is:
{{ struct} []}
[]
But when I change the "data" elements to uppercase, like this:
package main
import (
"encoding/xml"
"fmt"
)
type Data struct {
XMLName xml.Name `xml:"Data"`
Nam string `xml:"nam,attr"`
}
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data
}
func main() {
x := `
<struct>
<Data nam="MESSAGE_TYPE">
</Data>
<Data nam="MESSAGE_TYPE2">
</Data>
</struct>
`
s := Struct{}
err := xml.Unmarshal([]byte(x), &s)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", s)
fmt.Println(s.Data)
}
I got this:
{{ struct} [{{ Data} MESSAGE_TYPE} {{ Data} MESSAGE_TYPE2}]}
[{{ Data} MESSAGE_TYPE} {{ Data} MESSAGE_TYPE2}]
Can anybody tell me why?
答案1
得分: 7
如果您在结构字段上没有放置XML注释,字段的名称将被视为XML元素的名称。
在encoding/xml包的Unmarshal文档中,我们可以找到以下内容:
Unmarshal使用以下规则将XML元素映射到结构体。在规则中,字段的标签指的是结构体字段标签中与键'xml'关联的值(参见上面的示例)。
- 如果XML元素包含一个子元素,其名称与没有任何模式标志(",attr"、",chardata"等)的字段匹配,Unmarshal将该子元素映射到该结构体字段。
匹配是区分大小写的,所以在您的情况下会有所区别。
我建议像这样对结构进行注释以适应实际数据:
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data `xml:"data"`
}
英文:
If you do not put XML annotation on the structure field the name of the field is taken as name of XML element.
In the documentation on Unmarshal in endoding/xml package we can find the following:
> Unmarshal maps an XML element to a struct using the following rules. In the rules, the tag of a field refers to the value associated with the key 'xml' in the struct field's tag (see the example above).
> - If the XML element contains a sub-element whose name matches a
field without any mode flags (",attr", ",chardata", etc), Unmarshal
maps the sub-element to that struct field.
The matching is case sensitive so it makes a difference in your case.
I recommend annotating the structure like this to fit the actual data:
type Struct struct {
XMLName xml.Name `xml:"struct"`
Data []Data `xml:"data"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论