golang xml parsing with CDATA and a colon in the keyname

2,976 views
Skip to first unread message

Sankar

unread,
Jul 24, 2012, 12:25:58 PM7/24/12
to golan...@googlegroups.com
Hi,

I have the following go code.

type Root struct {
Source Source 
}

type Source struct {
Data string `xml:"encoding"`
}

func main() {
x := `<root><source:encoding><![CDATA[Hello]]></source:encoding></root>`

r := Root {}
_ = xml.Unmarshal([]byte(x), &r)
fmt.Println(r.Source)
}

I want to get the string "Hello" into the Source member (by changing it to string) or Data member. What should I do for this ? If the "<source" xml element does not have the ":encoding" part in it, I can successfully get the string "Hello" read. Can someone help me with this ?

Thanks a lot

Sankar

Kyle Lemons

unread,
Jul 24, 2012, 12:42:11 PM7/24/12
to Sankar, golan...@googlegroups.com
source:encoding is not an element named "source," it's an element named "encoding" with a "source" namespace.


package main

import "fmt"
import "encoding/xml"

type Root struct {
Source Source `xml:"encoding"`
}

type Source struct {
Data string `xml:",chardata"`
}

func main() {
x := `<root><source:encoding><![CDATA[Hello]]></source:encoding></root>`
r := Root {}
xml.Unmarshal([]byte(x), &r)
fmt.Println(r)

Sankar P

unread,
Jul 24, 2012, 12:45:11 PM7/24/12
to Kyle Lemons, golan...@googlegroups.com
Got it :) Thanks.
--
Sankar P
http://psankar.blogspot.com
Reply all
Reply to author
Forward
0 new messages