FAQ
Hi,

I have to parse XML like this:

<entry>
<vars>
<var key="foo">bar</var>
<var key="foo2"><var key="hello">world</var></var>
</vars>
</entry>

into this

{
"foo": "bar",
"foo2": {"hello": "world"}
}

Any ideas how to do this? I am thinking about using
http://golang.org/pkg/encoding/xml/#Decoder.Token to parse this into
map[string]interface{} .

Also what is the simplest way to get XML between <vars></vars>? I try this:
http://play.golang.org/p/NKsPStD2Om , but it fails.

--

Search Discussions

  • Dmitri Melnikov at Dec 30, 2012 at 2:29 am
    One approach would be to make xml.Unmarshal() recursively build a tree of
    vars and then convert that tree to a map:
    http://play.golang.org/p/eA4_caHLyk

    As for the second question, the code fails because the "innerxml" tag
    doesn't work when a node name is specified (same with "chardata", "comment"
    and "any"). You can see the code that checks for this
    here: http://golang.org/src/pkg/encoding/xml/typeinfo.go#L140.
    To avoid that problem, specify a separate struct to hold the raw content
    like this: http://play.golang.org/p/tp0gkrAunb

    --
  • Philipp Schumann at Jan 2, 2013 at 11:47 am
    I can highly recommend:

    https://github.com/jteeuwen/go-pkg-xmlx

    On Saturday, December 29, 2012 11:59:26 AM UTC+1, Vladimir Mihailenco wrote:

    Hi,

    I have to parse XML like this:

    <entry>
    <vars>
    <var key="foo">bar</var>
    <var key="foo2"><var key="hello">world</var></var>
    </vars>
    </entry>

    into this

    {
    "foo": "bar",
    "foo2": {"hello": "world"}
    }

    Any ideas how to do this? I am thinking about using
    http://golang.org/pkg/encoding/xml/#Decoder.Token to parse this into
    map[string]interface{} .

    Also what is the simplest way to get XML between <vars></vars>? I try
    this: http://play.golang.org/p/NKsPStD2Om , but it fails.
    --
  • Clbanning at Jan 3, 2013 at 10:59 pm
    Perhaps this can help: https://github.com/clbanning/x2j
    On Saturday, December 29, 2012 4:59:26 AM UTC-6, Vladimir Mihailenco wrote:

    Hi,

    I have to parse XML like this:

    <entry>
    <vars>
    <var key="foo">bar</var>
    <var key="foo2"><var key="hello">world</var></var>
    </vars>
    </entry>

    into this

    {
    "foo": "bar",
    "foo2": {"hello": "world"}
    }

    Any ideas how to do this? I am thinking about using
    http://golang.org/pkg/encoding/xml/#Decoder.Token to parse this into
    map[string]interface{} .

    Also what is the simplest way to get XML between <vars></vars>? I try
    this: http://play.golang.org/p/NKsPStD2Om , but it fails.
    --
  • Clbanning at Jan 4, 2013 at 10:59 pm
    Added a function to retrieve a 'relative value' from the doc.

    If your XML is straight:
    <entry>
    <vars>
    <foo>bar</foo>
    <foo2>
    <hello>world</hello>
    </foo2>
    </vars>
    </entry>

    You'll get:
    {
    "foo": "bar",
    "foo2": {
    "hello": "world"
    }
    }

    With your original XML the values can still be extracted from either the
    parse tree or the generated map[string]interface{} value. It's just a
    little more laborious.
    On Thursday, January 3, 2013 4:59:28 PM UTC-6, clba...@gmail.com wrote:

    Perhaps this can help: https://github.com/clbanning/x2j
    On Saturday, December 29, 2012 4:59:26 AM UTC-6, Vladimir Mihailenco wrote:

    Hi,

    I have to parse XML like this:

    <entry>
    <vars>
    <var key="foo">bar</var>
    <var key="foo2"><var key="hello">world</var></var>
    </vars>
    </entry>

    into this

    {
    "foo": "bar",
    "foo2": {"hello": "world"}
    }

    Any ideas how to do this? I am thinking about using
    http://golang.org/pkg/encoding/xml/#Decoder.Token to parse this into
    map[string]interface{} .

    Also what is the simplest way to get XML between <vars></vars>? I try
    this: http://play.golang.org/p/NKsPStD2Om , but it fails.
    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 29, '12 at 11:06a
activeJan 4, '13 at 10:59p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase