@@ -650,7 +650,7 @@ func XmlCheckIsValid(b ...bool) {
650
650
xmlCheckIsValid = b [0 ]
651
651
return
652
652
}
653
- xmlCheckIsValid = ! xmlCheckIsValid
653
+ xmlCheckIsValid = ! xmlCheckIsValid
654
654
}
655
655
656
656
// Encode a Map as XML. The companion of NewMapXml().
@@ -1108,7 +1108,9 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
1108
1108
}
1109
1109
1110
1110
// simple element? Note: '#text" is an invalid XML tag.
1111
+ isComplex := false
1111
1112
if v , ok := vv ["#text" ]; ok && n + 1 == lenvv {
1113
+ // just the value and attributes
1112
1114
switch v .(type ) {
1113
1115
case string :
1114
1116
if xmlEscapeChars {
@@ -1119,6 +1121,8 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
1119
1121
case []byte :
1120
1122
if xmlEscapeChars {
1121
1123
v = escapeChars (string (v .([]byte )))
1124
+ } else {
1125
+ v = string (v .([]byte ))
1122
1126
}
1123
1127
}
1124
1128
if _ , err = b .WriteString (">" + fmt .Sprintf ("%v" , v )); err != nil {
@@ -1129,16 +1133,33 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
1129
1133
isSimple = true
1130
1134
break
1131
1135
} else if ok {
1132
- // Handle edge case where simple element with attributes
1133
- // is unmarshal'd using NewMapXml() where attribute prefix
1134
- // has been set to "".
1135
- // TODO(clb): should probably scan all keys for invalid chars.
1136
- return fmt .Errorf ("invalid attribute key label: #text - due to attributes not being prefixed" )
1136
+ // need to handle when there are subelements in addition to the simple element value
1137
+ // issue #90
1138
+ switch v .(type ) {
1139
+ case string :
1140
+ if xmlEscapeChars {
1141
+ v = escapeChars (v .(string ))
1142
+ } else {
1143
+ v = v .(string )
1144
+ }
1145
+ case []byte :
1146
+ if xmlEscapeChars {
1147
+ v = escapeChars (string (v .([]byte )))
1148
+ } else {
1149
+ v = string (v .([]byte ))
1150
+ }
1151
+ }
1152
+ if _ , err = b .WriteString (">" + fmt .Sprintf ("%v" , v )); err != nil {
1153
+ return err
1154
+ }
1155
+ isComplex = true
1137
1156
}
1138
1157
1139
1158
// close tag with possible attributes
1140
- if _ , err = b .WriteString (">" ); err != nil {
1141
- return err
1159
+ if ! isComplex {
1160
+ if _ , err = b .WriteString (">" ); err != nil {
1161
+ return err
1162
+ }
1142
1163
}
1143
1164
if doIndent {
1144
1165
// *s += "\n"
@@ -1152,6 +1173,10 @@ func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value int
1152
1173
elemlist := make ([][2 ]interface {}, len (vv ))
1153
1174
n = 0
1154
1175
for k , v := range vv {
1176
+ if k == "#text" {
1177
+ // simple element handled above
1178
+ continue
1179
+ }
1155
1180
if lenAttrPrefix > 0 && lenAttrPrefix < len (k ) && k [:lenAttrPrefix ] == attrPrefix {
1156
1181
continue
1157
1182
}
0 commit comments