POST

Golang XML XML Analysis

My XML data:

<dictionary version="0.8" revision="403605"> <grammemes> <grammeme parent="">POST</grammeme> <grammeme parent="POST">NOUN</grammeme> </grammemes> </dictionary> 

My code is:

 type Dictionary struct { XMLName xml.Name `xml:"dictionary"` Grammemes *Grammemes `xml:"grammemes"` } type Grammemes struct { Grammemes []*Grammeme `xml:"grammeme"` } type Grammeme struct { Name string `xml:"grammeme"` Parent string `xml:"parent,attr"` } 

I get the Grammeme.Parent attribute, but I do not get the Grammeme.Name. Why?

+8
xml go
source share
1 answer

If you want the field to contain the contents of the current element, you can use the xml:",chardata" . The way you marked your structure is looking for the <grammeme> subelement <grammeme> .

Thus, one set of structures that you could decipher:

 type Dictionary struct { XMLName xml.Name `xml:"dictionary"` Grammemes []Grammeme `xml:"grammemes>grammeme"` } type Grammeme struct { Name string `xml:",chardata"` Parent string `xml:"parent,attr"` } 

You can check this example here: http://play.golang.org/p/7lQnQOCh0I

+9
source share

All Articles