File: books.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 (71 lines) | stat: -rw-r--r-- 1,771 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Note: this illustrates ValuesForKey() and ValuesForPath() methods

package main

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

var xmldata = []byte(`
   <books>
      <book seq="1">
         <author>William H. Gaddis</author>
         <title>The Recognitions</title>
         <review>One of the great seminal American novels of the 20th century.</review>
      </book>
      <book seq="2">
         <author>Austin Tappan Wright</author>
         <title>Islandia</title>
         <review>An example of earlier 20th century American utopian fiction.</review>
      </book>
      <book seq="3">
         <author>John Hawkes</author>
         <title>The Beetle Leg</title>
         <review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
      </book>
      <book seq="4">
         <author>T.E. Porter</author>
         <title>King's Day</title>
         <review>A magical novella.</review>
      </book>
   </books>
`)

func main() {
	fmt.Println(string(xmldata))

	m, err := mxj.NewMapXml(xmldata)
	if err != nil {
		log.Fatal("err:", err.Error())
	}

	v, _ := m.ValuesForKey("books")
	fmt.Println("path: books; len(v):", len(v))
	fmt.Printf("\t%+v\n", v)

	v, _ = m.ValuesForPath("books.book")
	fmt.Println("path: books.book; len(v):", len(v))
	for _, vv := range v {
		fmt.Printf("\t%+v\n", vv)
	}

	v, _ = m.ValuesForPath("books.*")
	fmt.Println("path: books.*; len(v):", len(v))
	for _, vv := range v {
		fmt.Printf("\t%+v\n", vv)
	}

	v, _ = m.ValuesForPath("books.*.title")
	fmt.Println("path: books.*.title len(v):", len(v))
	for _, vv := range v {
		fmt.Printf("\t%+v\n", vv)
	}

	v, _ = m.ValuesForPath("books.*.*")
	fmt.Println("path: books.*.*; len(v):", len(v))
	for _, vv := range v {
		fmt.Printf("\t%+v\n", vv)
	}
}