...

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

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

     1  // Note: this illustrates ValuesForKey() and ValuesForPath() methods
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"github.com/clbanning/mxj"
     8  	"log"
     9  )
    10  
    11  var xmldata = []byte(`
    12     <books>
    13        <book seq="1">
    14           <author>William H. Gaddis</author>
    15           <title>The Recognitions</title>
    16           <review>One of the great seminal American novels of the 20th century.</review>
    17        </book>
    18        <book seq="2">
    19           <author>Austin Tappan Wright</author>
    20           <title>Islandia</title>
    21           <review>An example of earlier 20th century American utopian fiction.</review>
    22        </book>
    23        <book seq="3">
    24           <author>John Hawkes</author>
    25           <title>The Beetle Leg</title>
    26           <review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
    27        </book>
    28        <book seq="4">
    29           <author>T.E. Porter</author>
    30           <title>King's Day</title>
    31           <review>A magical novella.</review>
    32        </book>
    33     </books>
    34  `)
    35  
    36  func main() {
    37  	fmt.Println(string(xmldata))
    38  
    39  	m, err := mxj.NewMapXml(xmldata)
    40  	if err != nil {
    41  		log.Fatal("err:", err.Error())
    42  	}
    43  
    44  	v, _ := m.ValuesForKey("books")
    45  	fmt.Println("path: books; len(v):", len(v))
    46  	fmt.Printf("\t%+v\n", v)
    47  
    48  	v, _ = m.ValuesForPath("books.book")
    49  	fmt.Println("path: books.book; len(v):", len(v))
    50  	for _, vv := range v {
    51  		fmt.Printf("\t%+v\n", vv)
    52  	}
    53  
    54  	v, _ = m.ValuesForPath("books.*")
    55  	fmt.Println("path: books.*; len(v):", len(v))
    56  	for _, vv := range v {
    57  		fmt.Printf("\t%+v\n", vv)
    58  	}
    59  
    60  	v, _ = m.ValuesForPath("books.*.title")
    61  	fmt.Println("path: books.*.title len(v):", len(v))
    62  	for _, vv := range v {
    63  		fmt.Printf("\t%+v\n", vv)
    64  	}
    65  
    66  	v, _ = m.ValuesForPath("books.*.*")
    67  	fmt.Println("path: books.*.*; len(v):", len(v))
    68  	for _, vv := range v {
    69  		fmt.Printf("\t%+v\n", vv)
    70  	}
    71  }
    72  

View as plain text