...

Source file src/github.com/clbanning/mxj/v2/examples/gonuts4.go

Documentation: github.com/clbanning/mxj/v2/examples

     1  // https://groups.google.com/forum/#!topic/golang-nuts/-N9Toa6qlu8
     2  // shows that you can extract attribute values directly from tag/key path.
     3  // NOTE: attribute values are encoded by prepending a hyphen, '-'.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"github.com/clbanning/mxj"
    10  )
    11  
    12  var xmldata = []byte(`
    13  	<doc>
    14  		<some_tag>
    15  			<geoInfo>
    16  				<city name="SEATTLE"/>
    17  				<state name="WA"/>
    18  				<country name="USA"/>
    19  			</geoInfo>
    20  			<geoInfo>
    21  				<city name="VANCOUVER"/>
    22  				<state name="BC"/>
    23  				<country name="CA"/>
    24  			</geoInfo>
    25  			<geoInfo>
    26  				<city name="LONDON"/>
    27  				<country name="UK"/>
    28  			</geoInfo>
    29  		</some_tag>
    30  	</doc>`)
    31  
    32  func main() {
    33  	fmt.Println("xmldata:", string(xmldata))
    34  
    35  	m, merr := mxj.NewMapXml(xmldata)
    36  	if merr != nil {
    37  		fmt.Println("merr:", merr)
    38  		return
    39  	}
    40  
    41  	// Attributes are keys with prepended hyphen, '-'.
    42  	values, err := m.ValuesForPath("doc.some_tag.geoInfo.country.-name")
    43  	if err != nil {
    44  		fmt.Println("err:", err.Error())
    45  	}
    46  
    47  	for _, v := range values {
    48  		fmt.Println("v:", v)
    49  	}
    50  }
    51  

View as plain text