FAQ
I move this issue to here, because I asked but noone reply so I am very
appriciated if someone can help me
you can find reference here:
http://stackoverflow.com/questions/20600137/golang-parse-both-xml-element-value-and-attributes-for-groups/
I express again

<RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
     <Item active="1" status="new" itemid="451254">
         <SubItem active="1" recent="false" usertext="No idea" id="78421">
             <SubItemField active="1" ready="no" type="1">1.4</SubItemField>
             <SubItemField active="1" ready="yes" type="2">4.5</SubItemField>
         </SubItem>
     </Item></RootLevel>

type SubItemField struct {
     Value float32 `xml:",chardata"`
     Active bool `xml:"active,attr"`
     Ready string `xml:"ready,attr"`
}

We can use chardata to get the value but the question is how about if it's
another struct that is defined inside, like this

<RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
     <Item active="1" status="new" itemid="451254">
         <SubItem active="1" recent="false" usertext="No idea" id="78421">
             <SubItemField active="1" ready="no" type="1"><Tag>1.4</Tag></SubItemField>
             <SubItemField active="1" ready="yes" type="2"><Tag>1.5</Tag></SubItemField>
         </SubItem>
     </Item></RootLevel>


type SubItemField struct {
     Value []Tag `xml:",chardata"` <===== bug here
     Active bool `xml:"active,attr"`
     Ready string `xml:"ready,attr"`
}

type Tag struct {
     Value float32 `xml:"Tag"`
}

Chardata didnt work here, I used `xml:",any"` but it's not different
Anyone can please help me?





--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Matt Harden at Dec 22, 2014 at 3:21 am
    This should work:

    type SubItemField struct {
         Value float32 `xml:"Tag"`
         Active bool `xml:"active,attr"`
         Ready string `xml:"ready,attr"`
    }


    On Sun Dec 21 2014 at 7:29:27 PM Hoai Ninh wrote:

    I move this issue to here, because I asked but noone reply so I am very
    appriciated if someone can help me
    you can find reference here:
    http://stackoverflow.com/questions/20600137/golang-parse-both-xml-element-value-and-attributes-for-groups/
    I express again

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1">1.4</SubItemField>
    <SubItemField active="1" ready="yes" type="2">4.5</SubItemField>
    </SubItem>
    </Item></RootLevel>

    type SubItemField struct {
    Value float32 `xml:",chardata"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    We can use chardata to get the value but the question is how about if
    it's another struct that is defined inside, like this

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1"><Tag>1.4</Tag></SubItemField>
    <SubItemField active="1" ready="yes" type="2"><Tag>1.5</Tag></SubItemField>
    </SubItem>
    </Item></RootLevel>


    type SubItemField struct {
    Value []Tag `xml:",chardata"` <===== bug here
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    type Tag struct {
    Value float32 `xml:"Tag"`
    }

    Chardata didnt work here, I used `xml:",any"` but it's not different
    Anyone can please help me?





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hoai Ninh at Dec 22, 2014 at 3:34 pm
    NO, it's Tag struct, not simply float32
    On Monday, December 22, 2014 10:21:36 AM UTC+7, Matt Harden wrote:

    This should work:

    type SubItemField struct {
    Value float32 `xml:"Tag"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }



    On Sun Dec 21 2014 at 7:29:27 PM Hoai Ninh <nin...@gmail.com <javascript:>>
    wrote:
    I move this issue to here, because I asked but noone reply so I am very
    appriciated if someone can help me
    you can find reference here:
    http://stackoverflow.com/questions/20600137/golang-parse-both-xml-element-value-and-attributes-for-groups/
    I express again

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1">1.4</SubItemField>
    <SubItemField active="1" ready="yes" type="2">4.5</SubItemField>
    </SubItem>
    </Item></RootLevel>

    type SubItemField struct {
    Value float32 `xml:",chardata"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    We can use chardata to get the value but the question is how about if
    it's another struct that is defined inside, like this

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1"><Tag>1.4</Tag></SubItemField>
    <SubItemField active="1" ready="yes" type="2"><Tag>1.5</Tag></SubItemField>
    </SubItem>
    </Item></RootLevel>


    type SubItemField struct {
    Value []Tag `xml:",chardata"` <===== bug here
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    type Tag struct {
    Value float32 `xml:"Tag"`
    }

    Chardata didnt work here, I used `xml:",any"` but it's not different
    Anyone can please help me?





    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Dec 23, 2014 at 12:45 am
    I'm not sure what you mean; I believe the struct I listed would parse the
    sample XML you posted. In any case, if you need it to be a separate struct,
    you can do this:

    type SubItemField struct {
         Tag Tag // `xml:"Tag"` tag is not necessary
         Active bool `xml:"active,attr"`
         Ready string `xml:"ready,attr"`
    }
    type Tag struct { Value float32 `xml:",chardata"`}


    On Mon Dec 22 2014 at 9:34:12 AM Hoai Ninh wrote:

    NO, it's Tag struct, not simply float32

    On Monday, December 22, 2014 10:21:36 AM UTC+7, Matt Harden wrote:

    This should work:

    type SubItemField struct {
    Value float32 `xml:"Tag"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }



    On Sun Dec 21 2014 at 7:29:27 PM Hoai Ninh wrote:
    I move this issue to here, because I asked but noone reply so I am very
    appriciated if someone can help me
    you can find reference here: http://stackoverflow.com/
    questions/20600137/golang-parse-both-xml-element-value-
    and-attributes-for-groups/
    I express again

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1">1.4</SubItemField>
    <SubItemField active="1" ready="yes" type="2">4.5</SubItemField>
    </SubItem>
    </Item></RootLevel>

    type SubItemField struct {
    Value float32 `xml:",chardata"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    We can use chardata to get the value but the question is how about if
    it's another struct that is defined inside, like this

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1"><Tag>1.4</Tag></SubItemField>
    <SubItemField active="1" ready="yes" type="2"><Tag>1.5</Tag></SubItemField>
    </SubItem>
    </Item></RootLevel>


    type SubItemField struct {
    Value []Tag `xml:",chardata"` <===== bug here
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    type Tag struct {
    Value float32 `xml:"Tag"`
    }

    Chardata didnt work here, I used `xml:",any"` but it's not different
    Anyone can please help me?





    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hoai Ninh at Dec 23, 2014 at 1:09 am
    I mean how about if inside tag is a struct instead of
    Like this

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
         <Item active="1" status="new" itemid="451254">
             <SubItem active="1" recent="false" usertext="No idea" id="78421">
                 <SubItemField active="1" ready="no" type="1">
                     <Tag>1.4</Tag>
                     <AnotherTag attr="attr1">
                         <Value>Something1</Value>
                     </AnotherTag>
                 </SubItemField>
                 <SubItemField active="1" ready="yes" type="2">
                     <Tag>1.5</Tag>
                     <AnotherTag attr="attr1">
                         <Value>Something2</Value>
                     </AnotherTag>
                 </SubItemField>
             </SubItem>
         </Item>
    </RootLevel>

    So how can I get *Tag* value and value inside *AnotherTag*
    regards
    On Tuesday, December 23, 2014 7:46:03 AM UTC+7, Matt Harden wrote:

    I'm not sure what you mean; I believe the struct I listed would parse the
    sample XML you posted. In any case, if you need it to be a separate struct,
    you can do this:

    type SubItemField struct {
    Tag Tag // `xml:"Tag"` tag is not necessary
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }
    type Tag struct { Value float32 `xml:",chardata"`}



    On Mon Dec 22 2014 at 9:34:12 AM Hoai Ninh <nin...@gmail.com <javascript:>>
    wrote:
    NO, it's Tag struct, not simply float32

    On Monday, December 22, 2014 10:21:36 AM UTC+7, Matt Harden wrote:

    This should work:

    type SubItemField struct {
    Value float32 `xml:"Tag"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }



    On Sun Dec 21 2014 at 7:29:27 PM Hoai Ninh wrote:
    I move this issue to here, because I asked but noone reply so I am very
    appriciated if someone can help me
    you can find reference here: http://stackoverflow.com/
    questions/20600137/golang-parse-both-xml-element-value-
    and-attributes-for-groups/
    I express again

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1">1.4</SubItemField>
    <SubItemField active="1" ready="yes" type="2">4.5</SubItemField>
    </SubItem>
    </Item></RootLevel>

    type SubItemField struct {
    Value float32 `xml:",chardata"`
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    We can use chardata to get the value but the question is how about if
    it's another struct that is defined inside, like this

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1"><Tag>1.4</Tag></SubItemField>
    <SubItemField active="1" ready="yes" type="2"><Tag>1.5</Tag></SubItemField>
    </SubItem>
    </Item></RootLevel>


    type SubItemField struct {
    Value []Tag `xml:",chardata"` <===== bug here
    Active bool `xml:"active,attr"`
    Ready string `xml:"ready,attr"`
    }

    type Tag struct {
    Value float32 `xml:"Tag"`
    }

    Chardata didnt work here, I used `xml:",any"` but it's not different
    Anyone can please help me?





    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hoai Ninh at Dec 23, 2014 at 1:11 am
    I mean how about if inside tag is a struct instead of
    Like this

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
         <Item active="1" status="new" itemid="451254">
             <SubItem active="1" recent="false" usertext="No idea" id="78421">
                 <SubItemField active="1" ready="no" type="1">
                     <Tag>1.4</Tag>
                     <AnotherTag attr="attr1">
                         <Value>Something1</Value>
                     </AnotherTag>
                 </SubItemField>
                 <SubItemField active="1" ready="yes" type="2">
                     <Tag>1.5</Tag>
                     <AnotherTag attr="attr1">
                         <Value>Something2</Value>
                     </AnotherTag>
                 </SubItemField>
             </SubItem>
         </Item>
    </RootLevel>

    So how can I get *Tag* value and value inside *AnotherTag*
    regards

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Matt Harden at Dec 23, 2014 at 4:09 am
    I'm having a hard time understanding why you're struggling. It seems like
    you might be giving examples that don't really reflect what you're trying
    to do. It would help if you could post the *actual* XML you're trying to
    parse, and the values you would like to get out of it.

    In the case of the XML you gave in the last post, you could do this:
         http://play.golang.org/p/RVFnBB_wtC
    or this:
         http://play.golang.org/p/J52ksL6kdM

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com">
       <Item active="1" status="new" itemid="451254">
         <SubItem active="1" recent="false" usertext="No idea" id="78421">
           <SubItemField active="1" ready="no" type="1">
             <Tag>1.4</Tag>
             <AnotherTag attr="attr1">
               <Value>Something1</Value>
             </AnotherTag>
           </SubItemField>
           <SubItemField active="1" ready="yes" type="2">
             <Tag>1.5</Tag>
             <AnotherTag attr="attr1">
               <Value>Something2</Value>
             </AnotherTag>
           </SubItemField>
         </SubItem>
       </Item>
    </RootLevel>

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Hoai Ninh at Dec 24, 2014 at 1:22 am
    Dear Matt,

    it's working now, very appreciate you are still following up this topic, It
    helps me a lot.
    Appreciated

    Regards,



    On Tuesday, December 23, 2014 11:10:08 AM UTC+7, Matt Harden wrote:

    I'm having a hard time understanding why you're struggling. It seems like
    you might be giving examples that don't really reflect what you're trying
    to do. It would help if you could post the *actual* XML you're trying to
    parse, and the values you would like to get out of it.

    In the case of the XML you gave in the last post, you could do this:
    http://play.golang.org/p/RVFnBB_wtC
    or this:
    http://play.golang.org/p/J52ksL6kdM

    <RootLevel status="new" timestamp="1383259529" xmlns="http://someplace.com
    ">
    <Item active="1" status="new" itemid="451254">
    <SubItem active="1" recent="false" usertext="No idea" id="78421">
    <SubItemField active="1" ready="no" type="1">
    <Tag>1.4</Tag>
    <AnotherTag attr="attr1">
    <Value>Something1</Value>
    </AnotherTag>
    </SubItemField>
    <SubItemField active="1" ready="yes" type="2">
    <Tag>1.5</Tag>
    <AnotherTag attr="attr1">
    <Value>Something2</Value>
    </AnotherTag>
    </SubItemField>
    </SubItem>
    </Item>
    </RootLevel>
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 22, '14 at 1:29a
activeDec 24, '14 at 1:22a
posts8
users2
websitegolang.org

2 users in discussion

Hoai Ninh: 5 posts Matt Harden: 3 posts

People

Translate

site design / logo © 2021 Grokbase