xml.Unmarshal of subcollection of elements.

1,054 views
Skip to first unread message

Francisco Diaz Trepat - gmail

unread,
Apr 27, 2012, 3:22:01 PM4/27/12
to golang-nuts
Hi I cannot seem to do the following when the variable is not equal to the tag name

<elements id='1'>
    <element id='10'>
       <value>blah</value>
   </element>
    <element id='11'>
       <value>blah</value>
   </element>
    <element id='12'>
       <value>blah</value>
   </element>
    <element id='13'>
       <value>blah</value>
   </element>
    <element id='14'>
       <value>blah</value>
   </element>
</element>

type Elements struct {
   XMLName   xml.Name `xml:"elements"`
   Id                int            `xml:"id,attr"`
   Elements     []Element
}

type Element struct {
   XMLName   xml.Name `xml:"element"`
   Id                int            `xml:"id,attr"`
   Value          string        `xml:"value"`
}


f(t)

Kyle Lemons

unread,
Apr 27, 2012, 3:26:06 PM4/27/12
to Francisco Diaz Trepat - gmail, golang-nuts
On Fri, Apr 27, 2012 at 12:22 PM, Francisco Diaz Trepat - gmail <francisco....@gmail.com> wrote:
Hi I cannot seem to do the following when the variable is not equal to the tag name

<elements id='1'>
    <element id='10'>
       <value>blah</value>
   </element>
    <element id='11'>
       <value>blah</value>
   </element>
    <element id='12'>
       <value>blah</value>
   </element>
    <element id='13'>
       <value>blah</value>
   </element>
    <element id='14'>
       <value>blah</value>
   </element>
</element>

type Elements struct {
   XMLName   xml.Name `xml:"elements"`
 
   Id                int            `xml:"id,attr"`
   Elements     []Element

I think you want to name this Element or add `xml:"element"`

Francisco Diaz Trepat - gmail

unread,
Apr 27, 2012, 3:36:44 PM4/27/12
to golang-nuts
yep, but it does not work, I tried with `xml:"element"` defined in Elements struct, with XMLName define in Element struct and with both, and none of them.

I cannot get a struct Elements.Elements where .Elements is a slice of <element>.

I have not tried putting (in Elements.Element struct) `xml:elements>element"`

Any samples known to work?

On Fri, Apr 27, 2012 at 4:34 PM, Francisco Diaz Trepat - gmail <francisco....@gmail.com> wrote:
yep, but it does not work, I tried with `xml:"element"` defined in Elements struct, with XMLName define in Element struct and with both, and none of them.

I cannot get a struct Elements.Elements where .Elements is a slice of <element>.


Any samples known to work?

f(t)

Kyle Lemons

unread,
Apr 27, 2012, 4:03:42 PM4/27/12
to Francisco Diaz Trepat - gmail, golang-nuts

Francisco Diaz Trepat - gmail

unread,
Apr 28, 2012, 1:28:59 PM4/28/12
to Kyle Lemons, golang-nuts
:-)
Reply all
Reply to author
Forward
0 new messages