How to Unmarshal an XML document to a Map[string]string

6,228 views
Skip to first unread message

nvcnvn

unread,
Nov 18, 2011, 8:33:38 AM11/18/11
to golang-nuts
Is there any way read an XML document and but it to a
map[string]string? I have read about xml package but there only
example with struct.
For example:
#######################
<?xml version="1.0" encoding="UTF-8" ?>
<langs>
<en>English</en>
</langs>
#######################
or this file:
#######################
<?xml version="1.0" encoding="UTF-8" ?>
<langs>
<lang name="en">English</lang>
</langs>
#######################

I hope by some magical with xml pkg, I get a map: map["en"] ==
"English"

P/s: I can put it to a struct, after that loop over the struct range
and generate a map, but I need to find if there is one simple way!

chrisfarms

unread,
Nov 18, 2011, 1:04:17 PM11/18/11
to golan...@googlegroups.com
XML doesn't really have a consistant way of mapping to map[string]string type. Who would decide the standard for how to deal with attributes, how would it know when you want a collection of something (implying slices) and when you just want single elements returned. In practice this is not that useful so you won't find anything like that in the standard library.

Most of the time when working with XML the structure is well defined, this is where Marshal/Unmarshal works really well, so if you know the scheme of your XML upfront; use the struct approach from xml.Marshal/Unmarshal.

If you don't know the structure you are parsing or you need something more specialist, you will need to use xml.Parser to step over the elements (Tokens) yourself.

Here's a really rough example of how to parse your first example into a map[string]string using xml.Parser to get you started:

Nguyên Nguyễn Văn Cao

unread,
Aug 21, 2012, 8:49:16 AM8/21/12
to golan...@googlegroups.com, clba...@gmail.com
thank you, I will try it.

Vào 02:12:03 UTC+7 Thứ ba, ngày 21 tháng tám năm 2012, clba...@gmail.com đã viết:
check out: https://github.com/clbanning/x2j

wentao Xiong

unread,
Aug 10, 2022, 9:52:02 PM8/10/22
to golang-nuts
Hello~ Why I code it hits: could not import xml (no required module provides package "xml")?

Kurtis Rader

unread,
Aug 10, 2022, 10:49:51 PM8/10/22
to wentao Xiong, golang-nuts
On Wed, Aug 10, 2022 at 6:51 PM 'wentao Xiong' via golang-nuts <golan...@googlegroups.com> wrote:
Hello~ Why I code it hits: could not import xml (no required module provides package "xml")?

Note that you are replying to a discussion thread that is ten years old. It is better to start a new discussion thread in this type of situation. You also did not show us a minimal reproduction of your problem. Having said that, you would import that package by adding this to your program:

    import "encoding/xml"


 
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/56f09282-9152-439b-8f8e-6a8e0a8ac37dn%40googlegroups.com.


--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Beyond

unread,
Aug 10, 2022, 10:51:07 PM8/10/22
to Kurtis Rader, golang-nuts
Thanks~ I have solved this problem


------------------ 原始邮件 ------------------
发件人: "Kurtis Rader"<kra...@skepticism.us>;
发送时间: 2022年8月11日(星期四) 上午10:49
收件人: "Beyond"<12158...@qq.com>;
抄送: "golang-nuts"<golan...@googlegroups.com>;
主题: Re: [go-nuts] Re: How to Unmarshal an XML document to a Map[string]string
Reply all
Reply to author
Forward
0 new messages