Re: xml selectors, network

76 views
Skip to first unread message

Maarten Koopmans

unread,
Sep 10, 2012, 6:36:56 AM9/10/12
to golan...@googlegroups.com
Please ignore this post - I accidentally double posted (being new and awaiting moderation). My other question about xml is more to the point.

Op maandag 10 september 2012 11:28:27 UTC+2 schreef Maarten Koopmans het volgende:
Hi,

Coming from Scala I am looking at Go to port a custom WebDAV daemon I've written.

However... this will require parsing HTTP headers and xml documents (POSTed). HTTP looks ok from what I see, but what I've read from XML with Go's libraries seem to require 1) files and 2) document-to-struct mappings.

I'd really be looking for something that allows me to select a part of a document or tag/attribute as with Scala's \ and \\ functions (which resemble XPath). Any such thing in Go?

--Maarten

Kamil Kisiel

unread,
Sep 10, 2012, 12:30:30 PM9/10/12
to golan...@googlegroups.com
You don't need files. xml.NewDecoder accepts any io.Reader. 

The xml package's document->struct mapping allows you to select parts of a document via struct tags. For example if you have an element hierarchy:

<body>
<elem1>
<elem2>
foo
</elem2>
</elem1>
</body>

And you wanted to extract the text from elem2 in to a struct you could define it as:

type Thing struct {
    Elem2 string `xml:"body>elem1>elem2"`
}

I've found the library quite convenient to use.

Maarten Koopmans

unread,
Sep 10, 2012, 2:56:07 PM9/10/12
to golan...@googlegroups.com
Thank you, that's exactly what's needed. I'll experiment.

--Maarten

Op maandag 10 september 2012 18:30:30 UTC+2 schreef Kamil Kisiel het volgende:
Reply all
Reply to author
Forward
0 new messages