将XML字符串解码为结构体。

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

Decode XML string into struct

问题

我有以下的xml:

  1. <DOC>
  2. <SubGroup1>
  3. <Value1>ABC123</Value1>
  4. <Value2>ABC123</Value2>
  5. <Value3>ABC123</Value3>
  6. <Value4>ABC123</Value4>
  7. </SubGroup1>
  8. <SubGroup2>
  9. <TheTag MyTagAttr="ABC123">
  10. <Value1>ABC123</Value1>
  11. <Value2>ABC123</Value2>
  12. <Value3>ABC123</Value3>
  13. <Value4 MyTagAttr="ABC123">ABC123</Value4>
  14. <Value5>ABC123</Value5>
  15. <Value6>ABC123</Value6>
  16. <Value7>ABC123</Value7>
  17. <Value8>ABC123</Value8>
  18. <Value9>ABC123</Value9>
  19. </TheTag>
  20. </SubGroup2>
  21. </DOC>

我需要将其解码为以下结构体:

  1. type TheTag struct {
  2. XMLName xml.Name `xml:"SubGroup2>TheTag"`
  3. Value1 string `xml:"Value1"`
  4. Value2 string `xml:"Value2"`
  5. }

但是我无法正确地将这个子元素解码到结构体中。

我得到以下错误:

  1. error decoding message content: %!w(xml.UnmarshalError=expected element type <SubGroup2>TheTag> but have <DOC>)

我的代码在Go Playground上可用:https://go.dev/play/p/O688qTBARJm

提前感谢!

英文:

I've the following xml:

  1. &lt;DOC&gt;
  2. &lt;SubGroup1&gt;
  3. &lt;Value1&gt;ABC123&lt;/Value1&gt;
  4. &lt;Value2&gt;ABC123&lt;/Value2&gt;
  5. &lt;Value3&gt;ABC123&lt;/Value3&gt;
  6. &lt;Value4&gt;ABC123&lt;/Value4&gt;
  7. &lt;/SubGroup1&gt;
  8. &lt;SubGroup2&gt;
  9. &lt;TheTag MyTagAttr=&quot;ABC123&quot;&gt;
  10. &lt;Value1&gt;ABC123&lt;/Value1&gt;
  11. &lt;Value2&gt;ABC123&lt;/Value2&gt;
  12. &lt;Value3&gt;ABC123&lt;/Value3&gt;
  13. &lt;Value4 MyTagAttr=&quot;ABC123&quot;&gt;ABC123&lt;/Value4&gt;
  14. &lt;Value5&gt;ABC123&lt;/Value5&gt;
  15. &lt;Value6&gt;ABC123&lt;/Value6&gt;
  16. &lt;Value7&gt;ABC123&lt;/Value7&gt;
  17. &lt;Value8&gt;ABC123&lt;/Value8&gt;
  18. &lt;Value9&gt;ABC123&lt;/Value9&gt;
  19. &lt;/TheTag&gt;
  20. &lt;/SubGroup2&gt;
  21. &lt;/DOC&gt;

And I need to decode into this struct:

  1. type TheTag struct {
  2. XMLName xml.Name `xml:&quot;SubGroup2&gt;TheTag&quot;`
  3. Value1 string `xml:&quot;Value1&quot;`
  4. Value2 string `xml:&quot;Value2&quot;`
  5. }

But I'm not able to decode properly this subelement into the struct.

I'm getting the following error:

  1. error decoding message content: %!w(xml.UnmarshalError=expected element type &lt;SubGroup2&gt;TheTag&gt; but have &lt;DOC&gt;)

My code is available here on Go Playgroud: https://go.dev/play/p/O688qTBARJm

Thanks in advance!

答案1

得分: 2

你应该将标签移动到其他位置。

  1. type TheTag struct {
  2. XMLName xml.Name `xml:"DOC"`
  3. Value1 string `xml:"SubGroup2>TheTag>Value1"`
  4. Value2 string `xml:"SubGroup2>TheTag>Value2"`
  5. }
英文:

You should probably move the tags.

  1. type TheTag struct {
  2. XMLName xml.Name `xml:&quot;DOC&quot;`
  3. Value1 string `xml:&quot;SubGroup2&gt;TheTag&gt;Value1&quot;`
  4. Value2 string `xml:&quot;SubGroup2&gt;TheTag&gt;Value2&quot;`
  5. }

答案2

得分: 0

你可以使用库github.com/clbanning/mxj。

  1. xmlBytes := []byte("<DOC>...</DOC>")
  2. xmlStruct, err := mxj.NewMapXml(xmlBytes)
  3. if err != nil {
  4. // 处理错误
  5. }
  6. theTag, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag")
  7. if err != nil {
  8. // "TheTag" 不存在
  9. }
  10. theTagInterface := theTag.(map[string]interface{})
  11. myTagAttr := theTagInterface["-MyTagAttr"].(string)
  12. val4, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag.Value4")
  13. if err != nil {
  14. // "Value4" 不存在
  15. }
  16. theVal4Interface := val4.(map[string]interface{})
  17. myVal4Attr := theVal4Interface["-MyTagAttr"].(string)
英文:

You can use the library github.com/clbanning/mxj

  1. xmlBytes = []byte(&quot;&lt;DOC&gt;...&lt;/DOC&gt;&quot;)
  2. xmlStruct, err := mxj.NewMapXml(xmlBytes)
  3. if err != nil {
  4. // handle error
  5. }
  6. theTag, err := xmlStruct.ValuesForPath(&quot;DOC.SubGroup2.TheTag&quot;)
  7. if err != nil {
  8. // Do not exist &quot;TheTag&quot;
  9. }
  10. theTagInterface := theTag.(map[string]interface{})
  11. myTagAttr := theTagInterface[&quot;-MyTagAttr&quot;].(string)
  12. val4, err := xmlStruct.ValuesForPath(&quot;DOC.SubGroup2.TheTag.Value4&quot;)
  13. if err != nil {
  14. // Do not exist &quot;Value4&quot;
  15. }
  16. theVal4Interface := val4.(map[string]interface{})
  17. myVal4Attr := theVal4Interface[&quot;-MyTagAttr&quot;].(string)

huangapple
  • 本文由 发表于 2023年2月8日 20:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385699.html
匿名

发表评论

匿名网友

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

确定