File: gonuts4.go

package info (click to toggle)
golang-github-clbanning-mxj 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,200 kB
  • sloc: xml: 176; makefile: 4
file content (50 lines) | stat: -rw-r--r-- 1,013 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// https://groups.google.com/forum/#!topic/golang-nuts/-N9Toa6qlu8
// shows that you can extract attribute values directly from tag/key path.
// NOTE: attribute values are encoded by prepending a hyphen, '-'.

package main

import (
	"fmt"
	"github.com/clbanning/mxj/v2"
)

var xmldata = []byte(`
	<doc>
		<some_tag>
			<geoInfo>
				<city name="SEATTLE"/>
				<state name="WA"/>
				<country name="USA"/>
			</geoInfo>
			<geoInfo>
				<city name="VANCOUVER"/>
				<state name="BC"/>
				<country name="CA"/>
			</geoInfo>
			<geoInfo>
				<city name="LONDON"/>
				<country name="UK"/>
			</geoInfo>
		</some_tag>
	</doc>`)

func main() {
	fmt.Println("xmldata:", string(xmldata))

	m, merr := mxj.NewMapXml(xmldata)
	if merr != nil {
		fmt.Println("merr:", merr)
		return
	}

	// Attributes are keys with prepended hyphen, '-'.
	values, err := m.ValuesForPath("doc.some_tag.geoInfo.country.-name")
	if err != nil {
		fmt.Println("err:", err.Error())
	}

	for _, v := range values {
		fmt.Println("v:", v)
	}
}