...

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

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

     1  // https://groups.google.com/forum/#!searchin/golang-nuts/idnet$20netid/golang-nuts/guM3ZHHqSF0/K1pBpMqQSSwJ
     2  // http://play.golang.org/p/BFFDxphKYK
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"github.com/clbanning/mxj"
    10  	"io"
    11  )
    12  
    13  // Demo how to compensate for irregular tag labels in data.
    14  // Need to extract from an XML stream the values for "netid" and "idnet".
    15  // Solution: use a wildcard path "data.*" to anonymize the "netid" and "idnet" tags.
    16  
    17  var msg1 = []byte(`
    18  <?xml version="1.0" encoding="UTF-8"?>
    19  <data>
    20      <netid>
    21          <disable>no</disable>
    22          <text1>default:text</text1>
    23          <word1>default:word</word1>
    24      </netid>
    25  </data>
    26  `)
    27  
    28  var msg2 = []byte(`
    29  <?xml version="1.0" encoding="UTF-8"?>
    30  <data>
    31      <idnet>
    32          <disable>yes</disable>
    33          <text1>default:text</text1>
    34          <word1>default:word</word1>
    35      </idnet>
    36  </data>
    37  `)
    38  
    39  func main() {
    40  	// let's create a message stream
    41  	buf := new(bytes.Buffer)
    42  	// load a couple of messages into it
    43  	_, _ = buf.Write(msg1)
    44  	_, _ = buf.Write(msg2)
    45  
    46  	n := 0
    47  	for {
    48  		n++
    49  		// read the stream as Map values - quit on io.EOF
    50  		m, raw, merr := mxj.NewMapXmlReaderRaw(buf)
    51  		if merr != nil && merr != io.EOF {
    52  			// handle error - for demo we just print it and continue
    53  			fmt.Printf("msg: %d - merr: %s\n", n, merr.Error())
    54  			continue
    55  		} else if merr == io.EOF {
    56  			break
    57  		}
    58  		fmt.Println("\nMessage to parse:", string(raw))
    59  		fmt.Println("Map value for XML message:", m.StringIndent())
    60  
    61  		// get the values for "netid" or "idnet" key using path == "data.*"
    62  		values, _ := m.ValuesForPath("data.*")
    63  		fmt.Println("\nmsg:", n, "> path == data.* - got array of values, len:", len(values))
    64  		for i, val := range values {
    65  			fmt.Println("ValuesForPath result array member -", i, ":", val)
    66  			fmt.Println("              k:v pairs for array member:", i)
    67  			for key, val := range val.(map[string]interface{}) {
    68  				// You'd probably want to process the value, as appropriate.
    69  				// Here we just print it out.
    70  				fmt.Println("\t\t", key, ":", val)
    71  			}
    72  		}
    73  
    74  		// This shows what happens if you wildcard the value keys for "idnet" and "netid"
    75  		values, _ = m.ValuesForPath("data.*.*")
    76  		fmt.Println("\npath == data.*.* - got an array of values, len(v):", len(values))
    77  		fmt.Println("(Note: values returned by ValuesForPath are at maximum depth of the tree. So just have values.)")
    78  		for i, val := range values {
    79  			fmt.Println("ValuesForPath array member -", i, ":", val)
    80  		}
    81  	}
    82  }
    83  

View as plain text