xml parser get data

110 views
Skip to first unread message

Jérôme LAFORGE

unread,
Nov 28, 2023, 10:00:18 AM11/28/23
to golang-nuts
Hello,

I have this xml
data := []byte(`<xml1>
<xml2>hello</xml2>
<xml3><xml4>foobar</xml4></xml3>
</xml1>`)

That, I want to unmarhsall into xmlBody struct:
type (
xml3 struct {
Data string `xml:",innerxml"`
}

xmlBody struct {
XML2 string `xml:"xml2"`
XML3 xml3   `xml:"xml3"`
}
)

Unfortunately, I am not able to get <xml3><xml4>foobar</xml4></xml3> as string in x.XML3.Data.

Thanks in advance for your help.




Jason E. Aten

unread,
Nov 28, 2023, 9:26:27 PM11/28/23
to golang-nuts
Perhaps you can adapt this to your needs.

https://github.com/glycerine/xml2csv

by taking just the xml parsing part, and then doing what you want with the tree of tags.

Jérôme LAFORGE

unread,
Nov 29, 2023, 4:03:23 AM11/29/23
to golang-nuts
Thx Jason.

If helps someone, plz find how I manage this: https://go.dev/play/p/kLwRB3fuJdK

Jérôme LAFORGE

unread,
Nov 29, 2023, 4:16:19 AM11/29/23
to golang-nuts
with fix when no xml3 tag : https://go.dev/play/p/DaW7z7woHeW
Reply all
Reply to author
Forward
0 new messages