...

Source file src/github.com/clbanning/mxj/v2/_deprecate/stuff.go

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

     1  
     2  // ======================== newMapToXmlIndent
     3  
     4  func (mv Map) MarshalXml(rootTag ...string) ([]byte, error) {
     5  	m := map[string]interface{}(mv)
     6  	var err error
     7  	// s := new(string)
     8  	// b := new(strings.Builder)
     9  	b := new(bytes.Buffer)
    10  	p := new(pretty) // just a stub
    11  
    12  	if len(m) == 1 && len(rootTag) == 0 {
    13  		for key, value := range m {
    14  			// if it an array, see if all values are map[string]interface{}
    15  			// we force a new root tag if we'll end up with no key:value in the list
    16  			// so: key:[string_val, bool:true] --> <doc><key>string_val</key><bool>true</bool></doc>
    17  			switch value.(type) {
    18  			case []interface{}:
    19  				for _, v := range value.([]interface{}) {
    20  					switch v.(type) {
    21  					case map[string]interface{}: // noop
    22  					default: // anything else
    23  						err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p)
    24  						goto done
    25  					}
    26  				}
    27  			}
    28  			err = marshalMapToXmlIndent(false, b, key, value, p)
    29  		}
    30  	} else if len(rootTag) == 1 {
    31  		err = marshalMapToXmlIndent(false, b, rootTag[0], m, p)
    32  	} else {
    33  		err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p)
    34  	}
    35  done:
    36  	return b.Bytes(), err
    37  }
    38  

View as plain text