0

I have the following XML:

<wb:sources page="1" pages="1" per_page="50" total="28" xmlns:wb="http://www.worldbank.org"> <wb:source id="11"> <wb:name>Africa Development Indicators</wb:name> <wb:description /> <wb:url /> </wb:source> <wb:source id="31"> <wb:name>Country Policy and Institutional Assessment (CPIA) </wb:name> <wb:description /> <wb:url /> </wb:source> </wb:sources> 

My code for parsing the XML:

 type Source struct { Id string `xml:"id,attr"` Name string `xml"wb:name"` } type Sources struct { XMLName xml.Name `xml"wb:sources"` Sourcez []Source `xml"wb:source"` } func GetSources() (*Sources, error) { resp, err := http.Get(sourcesUrl) if err != nil { log.Fatalf("error %v", err) return nil, err } defer resp.Body.Close() s := new(Sources) body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Print(err) return nil, err } log.Printf("body %v", string(body)) xml.Unmarshal(body, &s) return s, nil } My code: sources, err := GetSources() if err != nil { log.Panic() } fmt.Printf("%v ", sources) 

Keep returning &{{http://www.worldbank.org sources} []} what I'm I doing wrong?

2 Answers 2

3

You should not use wb: in the structs.

Here is your example simplified and working: http://play.golang.org/p/fphHokLprT

Sign up to request clarification or add additional context in comments.

1 Comment

There's a typo in your simplified example and it does not actually parse the name, description, and URL data from the test data. Here's a working version: play.golang.org/p/n5JFUk6UC2
0

Changed the structs to:

type Source struct { Name string `xml:"name"` Description string `xml:"description"` Url string `xml:"url"` } type Sources struct { XMLName xml.Name `xml"sources"` SourceList []Source `xml:"source"` } 

and it worked!!

1 Comment

Im wondering, how could one get the attribute values, eg 1 from pages in pages="1" on wb:sources?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.