FAQ
Hi,

I'm trying to unmarshal an XML file (using Unmarshal from encoding/xml)
which has empty elements. I'd like to be able to unmarshal the XML below
into the Example1 struct, but xml.Unmarshal chokes on the empty element, or
any value that strconv.Parseint can't parse. Unmarshalling into the
Example2 struct works, but that's not what I need. I assumed that Unmarshal
would simply ignore anything that wouldn't fit instead of returning an
error - the documentation for Unmarshal says "Well-formed data that does
not fit into v is discarded". Should the Unmarshal function choke on empty
elements? What's the best way of handling this?

const exampleXml = `
<Example>
<Ints>
<Int>1</Int>
<Int>2</Int>
<Int />
</Ints>
</Example>
`

type Example1 struct {
Int []int `xml:"Ints>Int"`
}

type Example2 struct {
Int []string `xml:"Ints>Int"`
}

Working example is here:

http://play.golang.org/p/q7Nt4vgVxU

--
Dave

--
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/groups/opt_out.

Search Discussions

  • Peter Kleiweg at Jan 31, 2013 at 4:29 pm
    Op donderdag 31 januari 2013 03:06:20 UTC+1 schreef Dave het volgende:

    "Well-formed data that does not fit into v is discarded".
    >

    I think that doesn't apply hear. The data is not well-formed, and if it
    were, it would fit into v. The empty string is not an integer.

    --
    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/groups/opt_out.
  • Peter Kleiweg at Jan 31, 2013 at 4:36 pm
    Op donderdag 31 januari 2013 17:29:31 UTC+1 schreef Peter Kleiweg het
    volgende:
    Op donderdag 31 januari 2013 03:06:20 UTC+1 schreef Dave het volgende:

    "Well-formed data that does not fit into v is discarded".
    I think that doesn't apply hear. The data is not well-formed, and if it
    were, it would fit into v. The empty string is not an integer.
    The fact that it is an emtpy element does not make it invalid. Something
    like <Int>a</Int> would be invalid too.

    If you were unmarshalling floats instead of ints, this would be
    well-formed: <Float>NaN</Float> . But <Float>a</Float> and <Float /> would
    still be invalid.

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 31, '13 at 2:09a
activeJan 31, '13 at 4:36p
posts3
users2
websitegolang.org

2 users in discussion

Peter Kleiweg: 2 posts David Robinson: 1 post

People

Translate

site design / logo © 2021 Grokbase