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

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

Decode XML string into struct

问题

我有以下的xml:

<DOC>
<SubGroup1>
	<Value1>ABC123</Value1>
	<Value2>ABC123</Value2>
	<Value3>ABC123</Value3>
	<Value4>ABC123</Value4>
</SubGroup1>
<SubGroup2>
	<TheTag MyTagAttr="ABC123">
		<Value1>ABC123</Value1>
		<Value2>ABC123</Value2>
		<Value3>ABC123</Value3>
		<Value4 MyTagAttr="ABC123">ABC123</Value4>
		<Value5>ABC123</Value5>
		<Value6>ABC123</Value6>
		<Value7>ABC123</Value7>
		<Value8>ABC123</Value8>
		<Value9>ABC123</Value9>
	</TheTag>
</SubGroup2>
</DOC>

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

type TheTag struct {
    XMLName xml.Name `xml:"SubGroup2>TheTag"`

    Value1  string  `xml:"Value1"`
    Value2  string  `xml:"Value2"`
}

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

我得到以下错误:

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:

&lt;DOC&gt;
&lt;SubGroup1&gt;
	&lt;Value1&gt;ABC123&lt;/Value1&gt;
	&lt;Value2&gt;ABC123&lt;/Value2&gt;
	&lt;Value3&gt;ABC123&lt;/Value3&gt;
	&lt;Value4&gt;ABC123&lt;/Value4&gt;
&lt;/SubGroup1&gt;
&lt;SubGroup2&gt;
	&lt;TheTag MyTagAttr=&quot;ABC123&quot;&gt;
		&lt;Value1&gt;ABC123&lt;/Value1&gt;
		&lt;Value2&gt;ABC123&lt;/Value2&gt;
		&lt;Value3&gt;ABC123&lt;/Value3&gt;
		&lt;Value4 MyTagAttr=&quot;ABC123&quot;&gt;ABC123&lt;/Value4&gt;
		&lt;Value5&gt;ABC123&lt;/Value5&gt;
		&lt;Value6&gt;ABC123&lt;/Value6&gt;
		&lt;Value7&gt;ABC123&lt;/Value7&gt;
		&lt;Value8&gt;ABC123&lt;/Value8&gt;
		&lt;Value9&gt;ABC123&lt;/Value9&gt;
	&lt;/TheTag&gt;
&lt;/SubGroup2&gt;
&lt;/DOC&gt;

And I need to decode into this struct:

type TheTag struct {
    XMLName xml.Name `xml:&quot;SubGroup2&gt;TheTag&quot;`

    Value1  string  `xml:&quot;Value1&quot;`
    Value2  string  `xml:&quot;Value2&quot;`
}

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

I'm getting the following error:

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

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

type TheTag struct {
  XMLName xml.Name `xml:"DOC"`

  Value1 string `xml:"SubGroup2>TheTag>Value1"`
  Value2 string `xml:"SubGroup2>TheTag>Value2"`
}
英文:

You should probably move the tags.

type TheTag struct {
  XMLName xml.Name `xml:&quot;DOC&quot;`

  Value1 string `xml:&quot;SubGroup2&gt;TheTag&gt;Value1&quot;`
  Value2 string `xml:&quot;SubGroup2&gt;TheTag&gt;Value2&quot;`
}

答案2

得分: 0

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

xmlBytes := []byte("<DOC>...</DOC>")
xmlStruct, err := mxj.NewMapXml(xmlBytes)
if err != nil {
  // 处理错误
}
theTag, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag")
if err != nil {
  // "TheTag" 不存在
}
theTagInterface := theTag.(map[string]interface{})
myTagAttr := theTagInterface["-MyTagAttr"].(string)

val4, err := xmlStruct.ValuesForPath("DOC.SubGroup2.TheTag.Value4")
if err != nil {
  // "Value4" 不存在
}
theVal4Interface := val4.(map[string]interface{})
myVal4Attr := theVal4Interface["-MyTagAttr"].(string)
英文:

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

xmlBytes = []byte(&quot;&lt;DOC&gt;...&lt;/DOC&gt;&quot;)
xmlStruct, err := mxj.NewMapXml(xmlBytes)
if err != nil {
  // handle error
}
theTag, err := xmlStruct.ValuesForPath(&quot;DOC.SubGroup2.TheTag&quot;)
if err != nil {
  // Do not exist &quot;TheTag&quot;
}
theTagInterface := theTag.(map[string]interface{})
myTagAttr := theTagInterface[&quot;-MyTagAttr&quot;].(string)

val4, err := xmlStruct.ValuesForPath(&quot;DOC.SubGroup2.TheTag.Value4&quot;)
if err != nil {
  // Do not exist &quot;Value4&quot;
}
theVal4Interface := val4.(map[string]interface{})
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:

确定