Parsing XML without creating structs

3,671 views
Skip to first unread message

ace....@gmail.com

unread,
May 6, 2014, 4:32:04 PM5/6/14
to golan...@googlegroups.com
Hello all,

Is it possible to load and parse XML without having to create structs? I'm calling some legacy SOAP services (that return a boatload of data!) for some quick prototyping and creating structs here would be a huge waste of time.

Thanks!

Raffaele Sena

unread,
May 6, 2014, 5:47:07 PM5/6/14
to ace....@gmail.com, golan...@googlegroups.com
You can create structs that only contain the data you need. Everything else will be silently discarded.

-- Raffaele



--
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.

Martin Angers

unread,
May 6, 2014, 11:11:05 PM5/6/14
to golan...@googlegroups.com
use xml.Decoder to create a decoder from a Reader, and then call decoder.Token() in a loop to iterate through all the elements of the xml document. You will need a type switch on the token to do anything useful (e.g. on StartToken, EndToken,etc.).

Sorry for not providing code example, I'm typing on my tablet at the moment... Hopefully this will lead you in the right direction.

Robert Melton

unread,
May 7, 2014, 6:25:01 AM5/7/14
to ace....@gmail.com, golang-nuts
Ace--

You might find http://godoc.org/github.com/str1ngs/jflect useful.  There are a lot of little bits of Go that generate structs based on json off the wire. 


--
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.



--
Robert Melton | http://robertmelton.com

C Banning

unread,
May 7, 2014, 6:29:14 AM5/7/14
to golan...@googlegroups.com, ace....@gmail.com

ace....@gmail.com

unread,
May 8, 2014, 10:24:21 AM5/8/14
to golan...@googlegroups.com, ace....@gmail.com
Thanks a lot for all the suggestions everyone! I'll try them out and post an update here soon.

Gustavo Niemeyer

unread,
May 8, 2014, 11:04:53 PM5/8/14
to ace....@gmail.com, golan...@googlegroups.com

This might also help:

http://gopkg.in/xmlpath.v1

gustavo @ http://niemeyer.net

--

luc.hei...@gmail.com

unread,
May 9, 2014, 8:40:12 AM5/9/14
to golan...@googlegroups.com, ace....@gmail.com

On Tuesday, May 6, 2014 10:32:04 PM UTC+2, ace....@gmail.com wrote:
Is it possible to load and parse XML without having to create structs? I'm calling some legacy SOAP services (that return a boatload of data!) for some quick prototyping and creating structs here would be a huge waste of time.

I just released a library that I was only using privately so far, which allows to parse XML files using an event based API for maximum speed and smallest memory usage possible.


Maybe it could help.

-- 
Luc Heinrich 

Mateusz Czapliński

unread,
May 12, 2014, 7:20:48 AM5/12/14
to golan...@googlegroups.com


On Friday, May 9, 2014 2:40:12 PM UTC+2, luc.hei...@gmail.com wrote:
I just released a library that I was only using privately so far, which allows to parse XML files using an event based API for maximum speed and smallest memory usage possible.


Looks very interesting! One question, as I'm not quite sure: does the .On() method support deeper paths, like .On("foo/bar/baz",...) ? Also, I was very surprised with the quoted benchmark results saying it's faster than xml.Unmarshal! - but then it sounds plausible, given that at least the reflect package is not used I suppose. Also, just 108 lines, feels neat! :)

Thanks for taking the effort to publicize it, and with BSD license!
/Mateusz.

Luc Heinrich

unread,
May 12, 2014, 9:12:51 AM5/12/14
to Mateusz Czapliński, golan...@googlegroups.com
On 12 mai 2014, at 13:20, Mateusz Czapliński <czap...@gmail.com> wrote:

> Looks very interesting! One question, as I'm not quite sure: does the .On() method support deeper paths, like .On("foo/bar/baz",...) ?

Yes. The examples I provide in the README file use the $text event but you can definitely use any tag event and go as deep as you want.

> Also, I was very surprised with the quoted benchmark results saying it's faster than xml.Unmarshal! - but then it sounds plausible, given that at least the reflect package is not used I suppose.

Yeah, I was quite surprised myself and I’m still not totally sure if I did not forget something somewhere or if I really measured things correctly :)

> Also, just 108 lines, feels neat! :)

Yes, the point of it was really to be a very thin layer on top of the encoding/xml/Decoder.

> Thanks for taking the effort to publicize it, and with BSD license!

Glad you find it interesting :)

--
Luc Heinrich - l...@honk-honk.com

Reply all
Reply to author
Forward
0 new messages