...

Source file src/github.com/clbanning/mxj/v2/x2j-wrapper/x2junmarshal_test.go

Documentation: github.com/clbanning/mxj/v2/x2j-wrapper

     1  package x2j
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"encoding/xml"
     7  )
     8  
     9  func TestUnmarshal(t *testing.T) {
    10  	var doc = []byte(`<doc> <name>Mayer Hawthorne</name> <song> <title>A Long Time</title> <verse no="1"> <line no="1">Henry was a renegade</line> <line no="2">Didn't like to play it safe</line> </verse> </song> </doc>`)
    11  
    12  	fmt.Println("\nUnmarshal test ... *map[string]interface{}, *string")
    13  	m := make(map[string]interface{},0)
    14  	err := Unmarshal(doc,&m)
    15  	if err != nil {
    16  		fmt.Println("err:",err.Error())
    17  	}
    18  	fmt.Println("m:",m)
    19  
    20  	var s string
    21  	err = Unmarshal(doc,&s)
    22  	if err != nil {
    23  		fmt.Println("err:",err.Error())
    24  	}
    25  	fmt.Println("s:",s)
    26  }
    27  
    28  func TestStructValue(t *testing.T) {
    29  	var doc = []byte(`<info><name>clbanning</name><address>unknown</address></info>`)
    30  	type Info struct {
    31  		XMLName xml.Name `xml:"info"`
    32  		Name string      `xml:"name"`
    33  		Address string   `xml:"address"`
    34  	}
    35  
    36  	var myInfo Info
    37  
    38  	fmt.Println("\nUnmarshal test ... struct:",string(doc))
    39  	err := Unmarshal(doc,&myInfo)
    40  	if err != nil {
    41  		fmt.Println("err:",err.Error())
    42  	} else {
    43  		fmt.Printf("myInfo: %+v\n",myInfo)
    44  	}
    45  }
    46  
    47  func TestMapValue(t *testing.T) {
    48  	var doc = `<doc> <name>Mayer Hawthorne</name> <song> <title>A Long Time</title> <verse no="1"> <line no="1">Henry was a renegade</line> <line no="2">Didn't like to play it safe</line> </verse> </song> </doc>`
    49  
    50  	fmt.Println("\nTestMapValue of doc.song.verse w/ len(attrs) == 0.")
    51  	fmt.Println("doc:",doc)
    52  	v,err := DocValue(doc,"doc.song.verse")
    53  	if err != nil {
    54  		fmt.Println("err:",err.Error())
    55  	}
    56  	fmt.Println("result:",v)
    57  }
    58  

View as plain text