相同的 XML 标签具有不同属性的不同结构体

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

Different structs for same XML tag with different attributes

问题

假设我有这个XML:

  1. <something>
  2. <value type="item">
  3. ...
  4. </value>
  5. <value type="other">
  6. ...
  7. </value>
  8. </something>

我能否以某种方式将具有不同属性的值提取到结构体的不同字段中,例如:

  1. type Something struct {
  2. Item Item `xml:"value[type=item]"` // 元代码
  3. Other Other `xml:"value[type=other]"`
  4. }

这种做法可行吗?我应该使用什么作为xml:属性?

英文:

Let's say I have this XML:

  1. &lt;something&gt;
  2. &lt;value type=&quot;item&quot;&gt;
  3. ...
  4. &lt;/value&gt;
  5. &lt;value type=&quot;other&quot;&gt;
  6. ...
  7. &lt;/value&gt;
  8. &lt;/something&gt;

Can I somehow extract the value with different attributes to different items on my struct, like:

  1. type Something struct {
  2. Item Item `xml:&quot;value[type=item]&quot;` // metacode
  3. Other Other `xml:&quot;value[type=other]&quot;`
  4. }

Is it possible? What should I use as the xml: attribute?

答案1

得分: 1

我不认为直接将按属性值区分的项目列表映射到特定字段是可能的。你需要将其映射到一个切片,就像下面的示例中的Items切片一样:

  1. package main
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. )
  6. func main() {
  7. type Item struct {
  8. Type string `xml:"type,attr"`
  9. Value string `xml:",chardata"`
  10. }
  11. type Something struct {
  12. XMLName xml.Name `xml:"something"`
  13. Name string `xml:"name"`
  14. Items []Item `xml:"value"`
  15. }
  16. var v Something
  17. data := `
  18. <something>
  19. <name> toto </name>
  20. <value type="item"> my item </value>
  21. <value type="other"> my other value </value>
  22. </something>
  23. `
  24. err := xml.Unmarshal([]byte(data), &v)
  25. if err != nil {
  26. fmt.Printf("error: %v", err)
  27. return
  28. }
  29. fmt.Println(v)
  30. }

在解码后,你可以根据需要处理切片,提取值并将它们放入特定的字段中。

英文:

I do not think it is possible to directly map a list of items discriminated by an attribute value to specific fields. You need to map it to a slice, like in the following example (Items slice):

  1. package main
  2. import (
  3. &quot;encoding/xml&quot;
  4. &quot;fmt&quot;
  5. )
  6. func main() {
  7. type Item struct {
  8. Type string `xml:&quot;type,attr&quot;`
  9. Value string `xml:&quot;,chardata&quot;`
  10. }
  11. type Something struct {
  12. XMLName xml.Name `xml:&quot;something&quot;`
  13. Name string `xml:&quot;name&quot;`
  14. Items []Item `xml:&quot;value&quot;`
  15. }
  16. var v Something
  17. data := `
  18. &lt;something&gt;
  19. &lt;name&gt; toto &lt;/name&gt;
  20. &lt;value type=&quot;item&quot;&gt; my item &lt;/value&gt;
  21. &lt;value type=&quot;other&quot;&gt; my other value &lt;/value&gt;
  22. &lt;/something&gt;
  23. `
  24. err := xml.Unmarshal([]byte(data), &amp;v)
  25. if err != nil {
  26. fmt.Printf(&quot;error: %v&quot;, err)
  27. return
  28. }
  29. fmt.Println(v)
  30. }

Up to you to process the slice after the decoding to extract the values and put them in specific fields if needed.

huangapple
  • 本文由 发表于 2015年2月4日 09:23:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/28311908.html
匿名

发表评论

匿名网友

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

确定