...

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

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

     1  // https://groups.google.com/forum/#!topic/golang-nuts/V83jUKluLnM
     2  // http://play.golang.org/p/alWGk4MDBc
     3  
     4  // Here messsages come in one of three forms:
     5  //		<GetClaimStatusCodesResult>... list of ClaimStatusCodeRecord ...</GetClaimStatusCodeResult>
     6  //		<GetClaimStatusCodesResult>... one instance of ClaimStatusCodeRecord ...</GetClaimStatusCodeResult>
     7  //		<GetClaimStatusCodesResult>... empty element ...</GetClaimStatusCodeResult>
     8  //		ValuesForPath
     9  
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"github.com/clbanning/mxj"
    15  )
    16  
    17  var xmlmsg1 = []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    18  <s:Body>
    19  <GetClaimStatusCodesResponse xmlns="http://tempuri.org/">
    20  <GetClaimStatusCodesResult xmlns:a="http://schemas.datacontract.org/2004/07/MRA.Claim.WebService.Domain" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    21  <a:ClaimStatusCodeRecord>
    22  <a:Active>true</a:Active>
    23  <a:Code>A</a:Code>
    24  <a:Description>Initial Claim Review/Screening</a:Description>
    25  </a:ClaimStatusCodeRecord>
    26  <a:ClaimStatusCodeRecord>
    27  <a:Active>true</a:Active>
    28  <a:Code>B</a:Code>
    29  <a:Description>Initial Contact Made w/ Provider</a:Description>
    30  </a:ClaimStatusCodeRecord>
    31  </GetClaimStatusCodesResult>
    32  </GetClaimStatusCodesResponse>
    33  </s:Body>
    34  </s:Envelope>
    35  `)
    36  
    37  var xmlmsg2 = []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    38  <s:Body>
    39  <GetClaimStatusCodesResponse xmlns="http://tempuri.org/">
    40  <GetClaimStatusCodesResult xmlns:a="http://schemas.datacontract.org/2004/07/MRA.Claim.WebService.Domain" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    41  <a:ClaimStatusCodeRecord>
    42  <a:Active>true</a:Active>
    43  <a:Code>A</a:Code>
    44  <a:Description>Initial Claim Review/Screening</a:Description>
    45  </a:ClaimStatusCodeRecord>
    46  </GetClaimStatusCodesResult>
    47  </GetClaimStatusCodesResponse>
    48  </s:Body>
    49  </s:Envelope>
    50  `)
    51  
    52  var xmlmsg3 = []byte(`<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    53  <s:Body>
    54  <GetClaimStatusCodesResponse xmlns="http://tempuri.org/">
    55  <GetClaimStatusCodesResult xmlns:a="http://schemas.datacontract.org/2004/07/MRA.Claim.WebService.Domain" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    56  </GetClaimStatusCodesResult>
    57  </GetClaimStatusCodesResponse>
    58  </s:Body>
    59  </s:Envelope>
    60  `)
    61  
    62  func main() {
    63  	xmldata := [][]byte{xmlmsg1, xmlmsg2, xmlmsg3}
    64  	fullPath(xmldata)
    65  	partPath1(xmlmsg1)
    66  	partPath2(xmlmsg1)
    67  	partPath3(xmlmsg1)
    68  	partPath4(xmlmsg1)
    69  	partPath5(xmlmsg1)
    70  	partPath6(xmlmsg1)
    71  }
    72  
    73  func fullPath(xmldata [][]byte) {
    74  	for i, msg := range xmldata {
    75  		fmt.Println("\ndoc:", i)
    76  		fmt.Println(string(msg))
    77  		// decode the XML
    78  		m, _ := mxj.NewMapXml(msg)
    79  
    80  		// get the value for the key path of interest
    81  		path := "Envelope.Body.GetClaimStatusCodesResponse.GetClaimStatusCodesResult.ClaimStatusCodeRecord"
    82  		values, err := m.ValuesForPath(path)
    83  		if err != nil {
    84  			fmt.Println("err:", err.Error())
    85  			return
    86  		}
    87  		if values == nil {
    88  			fmt.Println("path:", path)
    89  			fmt.Println("No ClaimStatusCodesResult code records.")
    90  			continue
    91  		}
    92  		fmt.Println("\nPath:", path)
    93  		fmt.Println("Number of code records:", len(values))
    94  		fmt.Println("values:", values, "\n")
    95  		for _, v := range values {
    96  			switch v.(type) {
    97  			case map[string]interface{}:
    98  				fmt.Println("map[string]interface{}:", v.(map[string]interface{}))
    99  			case []map[string]interface{}:
   100  				fmt.Println("[]map[string]interface{}:", v.([]map[string]interface{}))
   101  			case []interface{}:
   102  				fmt.Println("[]interface{}:", v.([]interface{}))
   103  			case interface{}:
   104  				fmt.Println("interface{}:", v.(interface{}))
   105  			}
   106  		}
   107  	}
   108  }
   109  
   110  func partPath1(msg []byte) {
   111  	fmt.Println("\nmsg:", string(msg))
   112  	m, _ := mxj.NewMapXml(msg)
   113  	fmt.Println("m:", m.StringIndent())
   114  	path := "Envelope.Body.*.*.ClaimStatusCodeRecord"
   115  	values, err := m.ValuesForPath(path)
   116  	if err != nil {
   117  		fmt.Println("err:", err.Error())
   118  		return
   119  	}
   120  	if values == nil {
   121  		fmt.Println("path:", path)
   122  		fmt.Println("No ClaimStatusCodesResult code records.")
   123  		return
   124  	}
   125  	fmt.Println("\nPath:", path)
   126  	fmt.Println("Number of code records:", len(values))
   127  	for n, v := range values {
   128  		fmt.Printf("\t#%d: %v\n", n, v)
   129  	}
   130  }
   131  
   132  func partPath2(msg []byte) {
   133  	fmt.Println("\nmsg:", string(msg))
   134  	m, _ := mxj.NewMapXml(msg)
   135  	fmt.Println("m:", m.StringIndent())
   136  	path := "Envelope.Body.*.*.*"
   137  	values, err := m.ValuesForPath(path)
   138  	if err != nil {
   139  		fmt.Println("err:", err.Error())
   140  		return
   141  	}
   142  	if values == nil {
   143  		fmt.Println("path:", path)
   144  		fmt.Println("No ClaimStatusCodesResult code records.")
   145  		return
   146  	}
   147  	fmt.Println("\nPath:", path)
   148  	fmt.Println("Number of code records:", len(values))
   149  	for n, v := range values {
   150  		fmt.Printf("\t#%d: %v\n", n, v)
   151  	}
   152  }
   153  
   154  func partPath3(msg []byte) {
   155  	fmt.Println("\nmsg:", string(msg))
   156  	m, _ := mxj.NewMapXml(msg)
   157  	fmt.Println("m:", m.StringIndent())
   158  	path := "*.*.*.*.*"
   159  	values, err := m.ValuesForPath(path)
   160  	if err != nil {
   161  		fmt.Println("err:", err.Error())
   162  		return
   163  	}
   164  	if values == nil {
   165  		fmt.Println("path:", path)
   166  		fmt.Println("No ClaimStatusCodesResult code records.")
   167  		return
   168  	}
   169  	fmt.Println("\nPath:", path)
   170  	fmt.Println("Number of code records:", len(values))
   171  	for n, v := range values {
   172  		fmt.Printf("\t#%d: %v\n", n, v)
   173  	}
   174  }
   175  
   176  func partPath4(msg []byte) {
   177  	fmt.Println("\nmsg:", string(msg))
   178  	m, _ := mxj.NewMapXml(msg)
   179  	fmt.Println("m:", m.StringIndent())
   180  	path := "*.*.*.*.*.Description"
   181  	values, err := m.ValuesForPath(path)
   182  	if err != nil {
   183  		fmt.Println("err:", err.Error())
   184  		return
   185  	}
   186  	if values == nil {
   187  		fmt.Println("path:", path)
   188  		fmt.Println("No ClaimStatusCodesResult code records.")
   189  		return
   190  	}
   191  	fmt.Println("\nPath:", path)
   192  	fmt.Println("Number of code records:", len(values))
   193  	for n, v := range values {
   194  		fmt.Printf("\t#%d: %v\n", n, v)
   195  	}
   196  }
   197  
   198  func partPath5(msg []byte) {
   199  	fmt.Println("\nmsg:", string(msg))
   200  	m, _ := mxj.NewMapXml(msg)
   201  	fmt.Println("m:", m.StringIndent())
   202  	path := "*.*.*.*.*.*"
   203  	values, err := m.ValuesForPath(path)
   204  	if err != nil {
   205  		fmt.Println("err:", err.Error())
   206  		return
   207  	}
   208  	if values == nil {
   209  		fmt.Println("path:", path)
   210  		fmt.Println("No ClaimStatusCodesResult code records.")
   211  		return
   212  	}
   213  	fmt.Println("\nPath:", path)
   214  	fmt.Println("Number of code records:", len(values))
   215  	for n, v := range values {
   216  		fmt.Printf("\t#%d: %v\n", n, v)
   217  	}
   218  }
   219  
   220  func partPath6(msg []byte) {
   221  	fmt.Println("\nmsg:", string(msg))
   222  	m, _ := mxj.NewMapXml(msg)
   223  	fmt.Println("m:", m.StringIndent())
   224  	path := "*.*.*.*.*.*.*"
   225  	values, err := m.ValuesForPath(path)
   226  	if err != nil {
   227  		fmt.Println("err:", err.Error())
   228  		return
   229  	}
   230  	if values == nil {
   231  		fmt.Println("path:", path)
   232  		fmt.Println("No ClaimStatusCodesResult code records.")
   233  		return
   234  	}
   235  	fmt.Println("\nPath:", path)
   236  	fmt.Println("Number of code records:", len(values))
   237  	for n, v := range values {
   238  		fmt.Printf("\t#%d: %v\n", n, v)
   239  	}
   240  }
   241  

View as plain text