Golang编组动态XML元素名称

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

Golang marshal dynamic xml element name

问题

XML文件由两个元素组成。这些元素具有相同的结构,除了一个元素名称不同。我尝试设置XMLName属性的值,但没有成功。

XML:

  1. <!-- 第一个元素 -->
  2. <PERSON>
  3. <ELEM1>...</ELEM1>
  4. <ELEM2>...</ELEM2>
  5. <ELEM3>...</ELEM3>
  6. <ELEM4>...</ELEM4>
  7. </PERSON>
  8. <!-- 第二个元素 -->
  9. <SENDER>
  10. <ELEM1>...</ELEM1>
  11. <ELEM2>...</ELEM2>
  12. <ELEM3>...</ELEM3>
  13. <ELEM4>...</ELEM4>
  14. </SENDER>

是否可以定义一个结构体,使元素名称是动态的?

  1. type Person struct {
  2. XMLName string `xml:"???"` // 如何使其动态?
  3. e1 string `xml:"ELEM1"`
  4. e2 string `xml:"ELEM2"`
  5. e3 string `xml:"ELEM3"`
  6. e4 string `xml:"ELEM4"`
  7. }
英文:

The xml file consists of two elements. Those elements have the same structure except for one element name. I tried to set a value to the XMLName property, but that didn't work.

Xml:

  1. &lt;!-- first element --&gt;
  2. &lt;PERSON&gt;
  3. &lt;ELEM1&gt;...&lt;/ELEM1&gt;
  4. &lt;ELEM2&gt;...&lt;/ELEM2&gt;
  5. &lt;ELEM3&gt;...&lt;/ELEM3&gt;
  6. &lt;ELEM4&gt;...&lt;/ELEM4&gt;
  7. &lt;/PERSON&gt;
  8. &lt;!-- second element --&gt;
  9. &lt;SENDER&gt;
  10. &lt;ELEM1&gt;...&lt;/ELEM1&gt;
  11. &lt;ELEM2&gt;...&lt;/ELEM2&gt;
  12. &lt;ELEM3&gt;...&lt;/ELEM3&gt;
  13. &lt;ELEM4&gt;...&lt;/ELEM4&gt;
  14. &lt;/SENDER&gt;

Is it possible to define a struct such that the element name is dynamic?

  1. type Person struct {
  2. XMLName string `xml:&quot;???&quot;` // How make this dynamic?
  3. e1 string `xml:&quot;ELEM1&quot;`
  4. e2 string `xml:&quot;ELEM2&quot;`
  5. e3 string `xml:&quot;ELEM3&quot;`
  6. e4 string `xml:&quot;ELEM4&quot;`
  7. }

答案1

得分: 10

文档中,它说XMLName字段必须是xml.Name类型。

  1. type Person struct {
  2. XMLName xml.Name
  3. E1 string `xml:"ELEM1"`
  4. // ...
  5. }

通过xml.NameLocal字段设置元素名称:

  1. person := Person {
  2. XMLName: xml.Name { Local: "Person" },
  3. // ...
  4. }

(另外,为了将其包含在XML输出中,E1 - E4必须是可导出的)。

Playground示例:http://play.golang.org/p/bzSutFF9Bo

英文:

In the documentation, it says that the XMLName field must be of type xml.Name.

  1. type Person struct {
  2. XMLName xml.Name
  3. E1 string `xml:&quot;ELEM1&quot;`
  4. // ...
  5. }

Set the element name via the Local field of xml.Name:

  1. person := Person {
  2. XMLName: xml.Name { Local: &quot;Person&quot; },
  3. // ...
  4. }

(Also, E1 - E4 must be exported in order to be included in the XML output).

Playground example: http://play.golang.org/p/bzSutFF9Bo

答案2

得分: 3

我找到了一个更简洁的解决方案,你只需要像这样为XMLName字段设置结构标签:

  1. type Person struct {
  2. XMLName xml.Name `xml:"Person"`
  3. E1 string `xml:"ELEM1"`
  4. // ...
  5. }
英文:

I found a lighter solution for this case where you only need to set the struct tag for XMLName field like this:

  1. type Person struct {
  2. XMLName xml.Name `xml:&quot;Person&quot;`
  3. E1 string `xml:&quot;ELEM1&quot;`
  4. // ...
  5. }

huangapple
  • 本文由 发表于 2014年11月11日 22:40:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/26867417.html
匿名

发表评论

匿名网友

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

确定