进行XML解析时,“看不到”任何字段。

huangapple go评论82阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;encoding/xml&quot;
	&quot;io/ioutil&quot;
)

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

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

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

func main() {

	dict := Dictionary{}

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

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

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

	dict.ignore = &quot;test&quot;

	out, err := xml.MarshalIndent(&amp;dict, &quot;  &quot;, &quot;    &quot;)
	fmt.Println(string(out)) // prints:   &lt;DICTIONARY&gt;&lt;/DICTIONARY&gt;

	// 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.

huangapple
  • 本文由 发表于 2013年9月30日 15:35:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/19088799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定