// https://groups.google.com/forum/#!searchin/golang-nuts/idnet$20netid/golang-nuts/guM3ZHHqSF0/K1pBpMqQSSwJ
// http://play.golang.org/p/BFFDxphKYK
package main
import (
"bytes"
"fmt"
"github.com/clbanning/mxj"
"io"
)
// Demo how to re-label a key using mv.NewMap().
// Need to normalize from an XML stream the tags "netid" and "idnet".
// Solution: make everything "netid".
var msg1 = []byte(`
no
default:text
default:word
`)
var msg2 = []byte(`
yes
default:text
default:word
`)
func main() {
// let's create a message stream
buf := new(bytes.Buffer)
// load a couple of messages into it
_, _ = buf.Write(msg1)
_, _ = buf.Write(msg2)
n := 0
for {
n++
// read the stream as Map values - quit on io.EOF
m, raw, merr := mxj.NewMapXmlReaderRaw(buf)
if merr != nil && merr != io.EOF {
// handle error - for demo we just print it and continue
fmt.Printf("msg: %d - merr: %s\n", n, merr.Error())
continue
} else if merr == io.EOF {
break
}
// the first keypair retains values if data correct
// the second keypair relabels "idnet" to "netid"
n, _ := m.NewMap("data.netid", "data.idnet:data.netid")
x, _ := n.XmlIndent("", " ")
fmt.Println("original value:", string(raw))
fmt.Println("new value:")
fmt.Println(string(x))
}
}