xml.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. // Copyright 2012-2016, 2018-2019 Charles Banning. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file
  4. // xml.go - basically the core of X2j for map[string]interface{} values.
  5. // NewMapXml, NewMapXmlReader, mv.Xml, mv.XmlWriter
  6. // see x2j and j2x for wrappers to provide end-to-end transformation of XML and JSON messages.
  7. package mxj
  8. import (
  9. "bytes"
  10. "encoding/json"
  11. "encoding/xml"
  12. "errors"
  13. "fmt"
  14. "io"
  15. "reflect"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // ------------------- NewMapXml & NewMapXmlReader ... -------------------------
  22. // If XmlCharsetReader != nil, it will be used to decode the XML, if required.
  23. // Note: if CustomDecoder != nil, then XmlCharsetReader is ignored;
  24. // set the CustomDecoder attribute instead.
  25. // import (
  26. // charset "code.google.com/p/go-charset/charset"
  27. // github.com/clbanning/mxj
  28. // )
  29. // ...
  30. // mxj.XmlCharsetReader = charset.NewReader
  31. // m, merr := mxj.NewMapXml(xmlValue)
  32. var XmlCharsetReader func(charset string, input io.Reader) (io.Reader, error)
  33. // NewMapXml - convert a XML doc into a Map
  34. // (This is analogous to unmarshalling a JSON string to map[string]interface{} using json.Unmarshal().)
  35. // If the optional argument 'cast' is 'true', then values will be converted to boolean or float64 if possible.
  36. //
  37. // Converting XML to JSON is a simple as:
  38. // ...
  39. // mapVal, merr := mxj.NewMapXml(xmlVal)
  40. // if merr != nil {
  41. // // handle error
  42. // }
  43. // jsonVal, jerr := mapVal.Json()
  44. // if jerr != nil {
  45. // // handle error
  46. // }
  47. //
  48. // NOTES:
  49. // 1. Declarations, directives, process instructions and comments are NOT parsed.
  50. // 2. The 'xmlVal' will be parsed looking for an xml.StartElement, so BOM and other
  51. // extraneous xml.CharData will be ignored unless io.EOF is reached first.
  52. // 3. If CoerceKeysToLower() has been called, then all key values will be lower case.
  53. // 4. If CoerceKeysToSnakeCase() has been called, then all key values will be converted to snake case.
  54. // 5. If DisableTrimWhiteSpace(b bool) has been called, then all values will be trimmed or not. 'true' by default.
  55. func NewMapXml(xmlVal []byte, cast ...bool) (Map, error) {
  56. var r bool
  57. if len(cast) == 1 {
  58. r = cast[0]
  59. }
  60. return xmlToMap(xmlVal, r)
  61. }
  62. // Get next XML doc from an io.Reader as a Map value. Returns Map value.
  63. // NOTES:
  64. // 1. Declarations, directives, process instructions and comments are NOT parsed.
  65. // 2. The 'xmlReader' will be parsed looking for an xml.StartElement, so BOM and other
  66. // extraneous xml.CharData will be ignored unless io.EOF is reached first.
  67. // 3. If CoerceKeysToLower() has been called, then all key values will be lower case.
  68. // 4. If CoerceKeysToSnakeCase() has been called, then all key values will be converted to snake case.
  69. func NewMapXmlReader(xmlReader io.Reader, cast ...bool) (Map, error) {
  70. var r bool
  71. if len(cast) == 1 {
  72. r = cast[0]
  73. }
  74. // We need to put an *os.File reader in a ByteReader or the xml.NewDecoder
  75. // will wrap it in a bufio.Reader and seek on the file beyond where the
  76. // xml.Decoder parses!
  77. if _, ok := xmlReader.(io.ByteReader); !ok {
  78. xmlReader = myByteReader(xmlReader) // see code at EOF
  79. }
  80. // build the map
  81. return xmlReaderToMap(xmlReader, r)
  82. }
  83. // Get next XML doc from an io.Reader as a Map value. Returns Map value and slice with the raw XML.
  84. // NOTES:
  85. // 1. Declarations, directives, process instructions and comments are NOT parsed.
  86. // 2. Due to the implementation of xml.Decoder, the raw XML off the reader is buffered to []byte
  87. // using a ByteReader. If the io.Reader is an os.File, there may be significant performance impact.
  88. // See the examples - getmetrics1.go through getmetrics4.go - for comparative use cases on a large
  89. // data set. If the io.Reader is wrapping a []byte value in-memory, however, such as http.Request.Body
  90. // you CAN use it to efficiently unmarshal a XML doc and retrieve the raw XML in a single call.
  91. // 3. The 'raw' return value may be larger than the XML text value.
  92. // 4. The 'xmlReader' will be parsed looking for an xml.StartElement, so BOM and other
  93. // extraneous xml.CharData will be ignored unless io.EOF is reached first.
  94. // 5. If CoerceKeysToLower() has been called, then all key values will be lower case.
  95. // 6. If CoerceKeysToSnakeCase() has been called, then all key values will be converted to snake case.
  96. func NewMapXmlReaderRaw(xmlReader io.Reader, cast ...bool) (Map, []byte, error) {
  97. var r bool
  98. if len(cast) == 1 {
  99. r = cast[0]
  100. }
  101. // create TeeReader so we can retrieve raw XML
  102. buf := make([]byte, 0)
  103. wb := bytes.NewBuffer(buf)
  104. trdr := myTeeReader(xmlReader, wb) // see code at EOF
  105. m, err := xmlReaderToMap(trdr, r)
  106. // retrieve the raw XML that was decoded
  107. b := wb.Bytes()
  108. if err != nil {
  109. return nil, b, err
  110. }
  111. return m, b, nil
  112. }
  113. // xmlReaderToMap() - parse a XML io.Reader to a map[string]interface{} value
  114. func xmlReaderToMap(rdr io.Reader, r bool) (map[string]interface{}, error) {
  115. // parse the Reader
  116. p := xml.NewDecoder(rdr)
  117. if CustomDecoder != nil {
  118. useCustomDecoder(p)
  119. } else {
  120. p.CharsetReader = XmlCharsetReader
  121. }
  122. return xmlToMapParser("", nil, p, r)
  123. }
  124. // xmlToMap - convert a XML doc into map[string]interface{} value
  125. func xmlToMap(doc []byte, r bool) (map[string]interface{}, error) {
  126. b := bytes.NewReader(doc)
  127. p := xml.NewDecoder(b)
  128. if CustomDecoder != nil {
  129. useCustomDecoder(p)
  130. } else {
  131. p.CharsetReader = XmlCharsetReader
  132. }
  133. return xmlToMapParser("", nil, p, r)
  134. }
  135. // ===================================== where the work happens =============================
  136. // PrependAttrWithHyphen. Prepend attribute tags with a hyphen.
  137. // Default is 'true'. (Not applicable to NewMapXmlSeq(), mv.XmlSeq(), etc.)
  138. // Note:
  139. // If 'false', unmarshaling and marshaling is not symmetric. Attributes will be
  140. // marshal'd as <attr_tag>attr</attr_tag> and may be part of a list.
  141. func PrependAttrWithHyphen(v bool) {
  142. if v {
  143. attrPrefix = "-"
  144. lenAttrPrefix = len(attrPrefix)
  145. return
  146. }
  147. attrPrefix = ""
  148. lenAttrPrefix = len(attrPrefix)
  149. }
  150. // Include sequence id with inner tags. - per Sean Murphy, murphysean84@gmail.com.
  151. var includeTagSeqNum bool
  152. // IncludeTagSeqNum - include a "_seq":N key:value pair with each inner tag, denoting
  153. // its position when parsed. This is of limited usefulness, since list values cannot
  154. // be tagged with "_seq" without changing their depth in the Map.
  155. // So THIS SHOULD BE USED WITH CAUTION - see the test cases. Here's a sample of what
  156. // you get.
  157. /*
  158. <Obj c="la" x="dee" h="da">
  159. <IntObj id="3"/>
  160. <IntObj1 id="1"/>
  161. <IntObj id="2"/>
  162. <StrObj>hello</StrObj>
  163. </Obj>
  164. parses as:
  165. {
  166. Obj:{
  167. "-c":"la",
  168. "-h":"da",
  169. "-x":"dee",
  170. "intObj":[
  171. {
  172. "-id"="3",
  173. "_seq":"0" // if mxj.Cast is passed, then: "_seq":0
  174. },
  175. {
  176. "-id"="2",
  177. "_seq":"2"
  178. }],
  179. "intObj1":{
  180. "-id":"1",
  181. "_seq":"1"
  182. },
  183. "StrObj":{
  184. "#text":"hello", // simple element value gets "#text" tag
  185. "_seq":"3"
  186. }
  187. }
  188. }
  189. */
  190. func IncludeTagSeqNum(b ...bool) {
  191. if len(b) == 0 {
  192. includeTagSeqNum = !includeTagSeqNum
  193. } else if len(b) == 1 {
  194. includeTagSeqNum = b[0]
  195. }
  196. }
  197. // all keys will be "lower case"
  198. var lowerCase bool
  199. // Coerce all tag values to keys in lower case. This is useful if you've got sources with variable
  200. // tag capitalization, and you want to use m.ValuesForKeys(), etc., with the key or path spec
  201. // in lower case.
  202. // CoerceKeysToLower() will toggle the coercion flag true|false - on|off
  203. // CoerceKeysToLower(true|false) will set the coercion flag on|off
  204. //
  205. // NOTE: only recognized by NewMapXml, NewMapXmlReader, and NewMapXmlReaderRaw functions as well as
  206. // the associated HandleXmlReader and HandleXmlReaderRaw.
  207. func CoerceKeysToLower(b ...bool) {
  208. if len(b) == 0 {
  209. lowerCase = !lowerCase
  210. } else if len(b) == 1 {
  211. lowerCase = b[0]
  212. }
  213. }
  214. // disableTrimWhiteSpace sets if the white space should be removed or not
  215. var disableTrimWhiteSpace bool
  216. var trimRunes = "\t\r\b\n "
  217. // DisableTrimWhiteSpace set if the white space should be trimmed or not. By default white space is always trimmed. If
  218. // no argument is provided, trim white space will be disabled.
  219. func DisableTrimWhiteSpace(b ...bool) {
  220. if len(b) == 0 {
  221. disableTrimWhiteSpace = true
  222. } else {
  223. disableTrimWhiteSpace = b[0]
  224. }
  225. if disableTrimWhiteSpace {
  226. trimRunes = "\t\r\b\n"
  227. } else {
  228. trimRunes = "\t\r\b\n "
  229. }
  230. }
  231. // 25jun16: Allow user to specify the "prefix" character for XML attribute key labels.
  232. // We do this by replacing '`' constant with attrPrefix var, replacing useHyphen with attrPrefix = "",
  233. // and adding a SetAttrPrefix(s string) function.
  234. var attrPrefix string = `-` // the default
  235. var lenAttrPrefix int = 1 // the default
  236. // SetAttrPrefix changes the default, "-", to the specified value, s.
  237. // SetAttrPrefix("") is the same as PrependAttrWithHyphen(false).
  238. // (Not applicable for NewMapXmlSeq(), mv.XmlSeq(), etc.)
  239. func SetAttrPrefix(s string) {
  240. attrPrefix = s
  241. lenAttrPrefix = len(attrPrefix)
  242. }
  243. // 18jan17: Allows user to specify if the map keys should be in snake case instead
  244. // of the default hyphenated notation.
  245. var snakeCaseKeys bool
  246. // CoerceKeysToSnakeCase changes the default, false, to the specified value, b.
  247. // Note: the attribute prefix will be a hyphen, '-', or what ever string value has
  248. // been specified using SetAttrPrefix.
  249. func CoerceKeysToSnakeCase(b ...bool) {
  250. if len(b) == 0 {
  251. snakeCaseKeys = !snakeCaseKeys
  252. } else if len(b) == 1 {
  253. snakeCaseKeys = b[0]
  254. }
  255. }
  256. // 10jan19: use of pull request #57 should be conditional - legacy code assumes
  257. // numeric values are float64.
  258. var castToInt bool
  259. // CastValuesToInt tries to coerce numeric valus to int64 or uint64 instead of the
  260. // default float64. Repeated calls with no argument will toggle this on/off, or this
  261. // handling will be set with the value of 'b'.
  262. func CastValuesToInt(b ...bool) {
  263. if len(b) == 0 {
  264. castToInt = !castToInt
  265. } else if len(b) == 1 {
  266. castToInt = b[0]
  267. }
  268. }
  269. // 05feb17: support processing XMPP streams (issue #36)
  270. var handleXMPPStreamTag bool
  271. // HandleXMPPStreamTag causes decoder to parse XMPP <stream:stream> elements.
  272. // If called with no argument, XMPP stream element handling is toggled on/off.
  273. // (See xmppStream_test.go for example.)
  274. // If called with NewMapXml, NewMapXmlReader, New MapXmlReaderRaw the "stream"
  275. // element will be returned as:
  276. // map["stream"]interface{}{map[-<attrs>]interface{}}.
  277. // If called with NewMapSeq, NewMapSeqReader, NewMapSeqReaderRaw the "stream"
  278. // element will be returned as:
  279. // map["stream:stream"]interface{}{map["#attr"]interface{}{map[string]interface{}}}
  280. // where the "#attr" values have "#text" and "#seq" keys. (See NewMapXmlSeq.)
  281. func HandleXMPPStreamTag(b ...bool) {
  282. if len(b) == 0 {
  283. handleXMPPStreamTag = !handleXMPPStreamTag
  284. } else if len(b) == 1 {
  285. handleXMPPStreamTag = b[0]
  286. }
  287. }
  288. // 21jan18 - decode all values as map["#text":value] (issue #56)
  289. var decodeSimpleValuesAsMap bool
  290. // DecodeSimpleValuesAsMap forces all values to be decoded as map["#text":<value>].
  291. // If called with no argument, the decoding is toggled on/off.
  292. //
  293. // By default the NewMapXml functions decode simple values without attributes as
  294. // map[<tag>:<value>]. This function causes simple values without attributes to be
  295. // decoded the same as simple values with attributes - map[<tag>:map["#text":<value>]].
  296. func DecodeSimpleValuesAsMap(b ...bool) {
  297. if len(b) == 0 {
  298. decodeSimpleValuesAsMap = !decodeSimpleValuesAsMap
  299. } else if len(b) == 1 {
  300. decodeSimpleValuesAsMap = b[0]
  301. }
  302. }
  303. // xmlToMapParser (2015.11.12) - load a 'clean' XML doc into a map[string]interface{} directly.
  304. // A refactoring of xmlToTreeParser(), markDuplicate() and treeToMap() - here, all-in-one.
  305. // We've removed the intermediate *node tree with the allocation and subsequent rescanning.
  306. func xmlToMapParser(skey string, a []xml.Attr, p *xml.Decoder, r bool) (map[string]interface{}, error) {
  307. if lowerCase {
  308. skey = strings.ToLower(skey)
  309. }
  310. if snakeCaseKeys {
  311. skey = strings.Replace(skey, "-", "_", -1)
  312. }
  313. // NOTE: all attributes and sub-elements parsed into 'na', 'na' is returned as value for 'skey' in 'n'.
  314. // Unless 'skey' is a simple element w/o attributes, in which case the xml.CharData value is the value.
  315. var n, na map[string]interface{}
  316. var seq int // for includeTagSeqNum
  317. // Allocate maps and load attributes, if any.
  318. // NOTE: on entry from NewMapXml(), etc., skey=="", and we fall through
  319. // to get StartElement then recurse with skey==xml.StartElement.Name.Local
  320. // where we begin allocating map[string]interface{} values 'n' and 'na'.
  321. if skey != "" {
  322. n = make(map[string]interface{}) // old n
  323. na = make(map[string]interface{}) // old n.nodes
  324. if len(a) > 0 {
  325. for _, v := range a {
  326. if snakeCaseKeys {
  327. v.Name.Local = strings.Replace(v.Name.Local, "-", "_", -1)
  328. }
  329. var key string
  330. key = attrPrefix + v.Name.Local
  331. if lowerCase {
  332. key = strings.ToLower(key)
  333. }
  334. if xmlEscapeCharsDecoder { // per issue#84
  335. v.Value = escapeChars(v.Value)
  336. }
  337. na[key] = cast(v.Value, r, key)
  338. }
  339. }
  340. }
  341. // Return XMPP <stream:stream> message.
  342. if handleXMPPStreamTag && skey == "stream" {
  343. n[skey] = na
  344. return n, nil
  345. }
  346. for {
  347. t, err := p.Token()
  348. if err != nil {
  349. if err != io.EOF {
  350. return nil, errors.New("xml.Decoder.Token() - " + err.Error())
  351. }
  352. return nil, err
  353. }
  354. switch t.(type) {
  355. case xml.StartElement:
  356. tt := t.(xml.StartElement)
  357. // First call to xmlToMapParser() doesn't pass xml.StartElement - the map key.
  358. // So when the loop is first entered, the first token is the root tag along
  359. // with any attributes, which we process here.
  360. //
  361. // Subsequent calls to xmlToMapParser() will pass in tag+attributes for
  362. // processing before getting the next token which is the element value,
  363. // which is done above.
  364. if skey == "" {
  365. return xmlToMapParser(tt.Name.Local, tt.Attr, p, r)
  366. }
  367. // If not initializing the map, parse the element.
  368. // len(nn) == 1, necessarily - it is just an 'n'.
  369. nn, err := xmlToMapParser(tt.Name.Local, tt.Attr, p, r)
  370. if err != nil {
  371. return nil, err
  372. }
  373. // The nn map[string]interface{} value is a na[nn_key] value.
  374. // We need to see if nn_key already exists - means we're parsing a list.
  375. // This may require converting na[nn_key] value into []interface{} type.
  376. // First, extract the key:val for the map - it's a singleton.
  377. // Note:
  378. // * if CoerceKeysToLower() called, then key will be lower case.
  379. // * if CoerceKeysToSnakeCase() called, then key will be converted to snake case.
  380. var key string
  381. var val interface{}
  382. for key, val = range nn {
  383. break
  384. }
  385. // IncludeTagSeqNum requests that the element be augmented with a "_seq" sub-element.
  386. // In theory, we don't need this if len(na) == 1. But, we don't know what might
  387. // come next - we're only parsing forward. So if you ask for 'includeTagSeqNum' you
  388. // get it on every element. (Personally, I never liked this, but I added it on request
  389. // and did get a $50 Amazon gift card in return - now we support it for backwards compatibility!)
  390. if includeTagSeqNum {
  391. switch val.(type) {
  392. case []interface{}:
  393. // noop - There's no clean way to handle this w/o changing message structure.
  394. case map[string]interface{}:
  395. val.(map[string]interface{})["_seq"] = seq // will overwrite an "_seq" XML tag
  396. seq++
  397. case interface{}: // a non-nil simple element: string, float64, bool
  398. v := map[string]interface{}{"#text": val}
  399. v["_seq"] = seq
  400. seq++
  401. val = v
  402. }
  403. }
  404. // 'na' holding sub-elements of n.
  405. // See if 'key' already exists.
  406. // If 'key' exists, then this is a list, if not just add key:val to na.
  407. if v, ok := na[key]; ok {
  408. var a []interface{}
  409. switch v.(type) {
  410. case []interface{}:
  411. a = v.([]interface{})
  412. default: // anything else - note: v.(type) != nil
  413. a = []interface{}{v}
  414. }
  415. a = append(a, val)
  416. na[key] = a
  417. } else {
  418. na[key] = val // save it as a singleton
  419. }
  420. case xml.EndElement:
  421. // len(n) > 0 if this is a simple element w/o xml.Attrs - see xml.CharData case.
  422. if len(n) == 0 {
  423. // If len(na)==0 we have an empty element == "";
  424. // it has no xml.Attr nor xml.CharData.
  425. // Note: in original node-tree parser, val defaulted to "";
  426. // so we always had the default if len(node.nodes) == 0.
  427. if len(na) > 0 {
  428. n[skey] = na
  429. } else {
  430. n[skey] = "" // empty element
  431. }
  432. } else if len(n) == 1 && len(na) > 0 {
  433. // it's a simple element w/ no attributes w/ subelements
  434. for _, v := range n {
  435. na["#text"] = v
  436. }
  437. n[skey] = na
  438. }
  439. return n, nil
  440. case xml.CharData:
  441. // clean up possible noise
  442. tt := strings.Trim(string(t.(xml.CharData)), trimRunes)
  443. if xmlEscapeCharsDecoder { // issue#84
  444. tt = escapeChars(tt)
  445. }
  446. if len(tt) > 0 {
  447. if len(na) > 0 || decodeSimpleValuesAsMap {
  448. na["#text"] = cast(tt, r, "#text")
  449. } else if skey != "" {
  450. n[skey] = cast(tt, r, skey)
  451. } else {
  452. // per Adrian (http://www.adrianlungu.com/) catch stray text
  453. // in decoder stream -
  454. // https://github.com/clbanning/mxj/pull/14#issuecomment-182816374
  455. // NOTE: CharSetReader must be set to non-UTF-8 CharSet or you'll get
  456. // a p.Token() decoding error when the BOM is UTF-16 or UTF-32.
  457. continue
  458. }
  459. }
  460. default:
  461. // noop
  462. }
  463. }
  464. }
  465. var castNanInf bool
  466. // Cast "Nan", "Inf", "-Inf" XML values to 'float64'.
  467. // By default, these values will be decoded as 'string'.
  468. func CastNanInf(b ...bool) {
  469. if len(b) == 0 {
  470. castNanInf = !castNanInf
  471. } else if len(b) == 1 {
  472. castNanInf = b[0]
  473. }
  474. }
  475. // cast - try to cast string values to bool or float64
  476. // 't' is the tag key that can be checked for 'not-casting'
  477. func cast(s string, r bool, t string) interface{} {
  478. if checkTagToSkip != nil && t != "" && checkTagToSkip(t) {
  479. // call the check-function here with 't[0]'
  480. // if 'true' return s
  481. return s
  482. }
  483. if r {
  484. // handle nan and inf
  485. if !castNanInf {
  486. switch strings.ToLower(s) {
  487. case "nan", "inf", "-inf":
  488. return s
  489. }
  490. }
  491. // handle numeric strings ahead of boolean
  492. if castToInt {
  493. if f, err := strconv.ParseInt(s, 10, 64); err == nil {
  494. return f
  495. }
  496. if f, err := strconv.ParseUint(s, 10, 64); err == nil {
  497. return f
  498. }
  499. }
  500. if castToFloat {
  501. if f, err := strconv.ParseFloat(s, 64); err == nil {
  502. return f
  503. }
  504. }
  505. // ParseBool treats "1"==true & "0"==false, we've already scanned those
  506. // values as float64. See if value has 't' or 'f' as initial screen to
  507. // minimize calls to ParseBool; also, see if len(s) < 6.
  508. if castToBool {
  509. if len(s) > 0 && len(s) < 6 {
  510. switch s[:1] {
  511. case "t", "T", "f", "F":
  512. if b, err := strconv.ParseBool(s); err == nil {
  513. return b
  514. }
  515. }
  516. }
  517. }
  518. }
  519. return s
  520. }
  521. // pull request, #59
  522. var castToFloat = true
  523. // CastValuesToFloat can be used to skip casting to float64 when
  524. // "cast" argument is 'true' in NewMapXml, etc.
  525. // Default is true.
  526. func CastValuesToFloat(b ...bool) {
  527. if len(b) == 0 {
  528. castToFloat = !castToFloat
  529. } else if len(b) == 1 {
  530. castToFloat = b[0]
  531. }
  532. }
  533. var castToBool = true
  534. // CastValuesToBool can be used to skip casting to bool when
  535. // "cast" argument is 'true' in NewMapXml, etc.
  536. // Default is true.
  537. func CastValuesToBool(b ...bool) {
  538. if len(b) == 0 {
  539. castToBool = !castToBool
  540. } else if len(b) == 1 {
  541. castToBool = b[0]
  542. }
  543. }
  544. // checkTagToSkip - switch to address Issue #58
  545. var checkTagToSkip func(string) bool
  546. // SetCheckTagToSkipFunc registers function to test whether the value
  547. // for a tag should be cast to bool or float64 when "cast" argument is 'true'.
  548. // (Dot tag path notation is not supported.)
  549. // NOTE: key may be "#text" if it's a simple element with attributes
  550. // or "decodeSimpleValuesAsMap == true".
  551. // NOTE: does not apply to NewMapXmlSeq... functions.
  552. func SetCheckTagToSkipFunc(fn func(string) bool) {
  553. checkTagToSkip = fn
  554. }
  555. // ------------------ END: NewMapXml & NewMapXmlReader -------------------------
  556. // ------------------ mv.Xml & mv.XmlWriter - from j2x ------------------------
  557. const (
  558. DefaultRootTag = "doc"
  559. )
  560. var useGoXmlEmptyElemSyntax bool
  561. // XmlGoEmptyElemSyntax() - <tag ...></tag> rather than <tag .../>.
  562. // Go's encoding/xml package marshals empty XML elements as <tag ...></tag>. By default this package
  563. // encodes empty elements as <tag .../>. If you're marshaling Map values that include structures
  564. // (which are passed to xml.Marshal for encoding), this will let you conform to the standard package.
  565. func XmlGoEmptyElemSyntax() {
  566. useGoXmlEmptyElemSyntax = true
  567. }
  568. // XmlDefaultEmptyElemSyntax() - <tag .../> rather than <tag ...></tag>.
  569. // Return XML encoding for empty elements to the default package setting.
  570. // Reverses effect of XmlGoEmptyElemSyntax().
  571. func XmlDefaultEmptyElemSyntax() {
  572. useGoXmlEmptyElemSyntax = false
  573. }
  574. // ------- issue #88 ----------
  575. // xmlCheckIsValid set switch to force decoding the encoded XML to
  576. // see if it is valid XML.
  577. var xmlCheckIsValid bool
  578. // XmlCheckIsValid forces the encoded XML to be checked for validity.
  579. func XmlCheckIsValid(b ...bool) {
  580. if len(b) == 1 {
  581. xmlCheckIsValid = b[0]
  582. return
  583. }
  584. xmlCheckIsValid = !xmlCheckIsValid
  585. }
  586. // Encode a Map as XML. The companion of NewMapXml().
  587. // The following rules apply.
  588. // - The key label "#text" is treated as the value for a simple element with attributes.
  589. // - Map keys that begin with a hyphen, '-', are interpreted as attributes.
  590. // It is an error if the attribute doesn't have a []byte, string, number, or boolean value.
  591. // - Map value type encoding:
  592. // > string, bool, float64, int, int32, int64, float32: per "%v" formating
  593. // > []bool, []uint8: by casting to string
  594. // > structures, etc.: handed to xml.Marshal() - if there is an error, the element
  595. // value is "UNKNOWN"
  596. // - Elements with only attribute values or are null are terminated using "/>".
  597. // - If len(mv) == 1 and no rootTag is provided, then the map key is used as the root tag, possible.
  598. // Thus, `{ "key":"value" }` encodes as "<key>value</key>".
  599. // - To encode empty elements in a syntax consistent with encoding/xml call UseGoXmlEmptyElementSyntax().
  600. // The attributes tag=value pairs are alphabetized by "tag". Also, when encoding map[string]interface{} values -
  601. // complex elements, etc. - the key:value pairs are alphabetized by key so the resulting tags will appear sorted.
  602. func (mv Map) Xml(rootTag ...string) ([]byte, error) {
  603. m := map[string]interface{}(mv)
  604. var err error
  605. b := new(bytes.Buffer)
  606. p := new(pretty) // just a stub
  607. if len(m) == 1 && len(rootTag) == 0 {
  608. for key, value := range m {
  609. // if it an array, see if all values are map[string]interface{}
  610. // we force a new root tag if we'll end up with no key:value in the list
  611. // so: key:[string_val, bool:true] --> <doc><key>string_val</key><bool>true</bool></doc>
  612. switch value.(type) {
  613. case []interface{}:
  614. for _, v := range value.([]interface{}) {
  615. switch v.(type) {
  616. case map[string]interface{}: // noop
  617. default: // anything else
  618. err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p)
  619. goto done
  620. }
  621. }
  622. }
  623. err = marshalMapToXmlIndent(false, b, key, value, p)
  624. }
  625. } else if len(rootTag) == 1 {
  626. err = marshalMapToXmlIndent(false, b, rootTag[0], m, p)
  627. } else {
  628. err = marshalMapToXmlIndent(false, b, DefaultRootTag, m, p)
  629. }
  630. done:
  631. if xmlCheckIsValid {
  632. d := xml.NewDecoder(bytes.NewReader(b.Bytes()))
  633. for {
  634. _, err = d.Token()
  635. if err == io.EOF {
  636. err = nil
  637. break
  638. } else if err != nil {
  639. return nil, err
  640. }
  641. }
  642. }
  643. return b.Bytes(), err
  644. }
  645. // The following implementation is provided only for symmetry with NewMapXmlReader[Raw]
  646. // The names will also provide a key for the number of return arguments.
  647. // Writes the Map as XML on the Writer.
  648. // See Xml() for encoding rules.
  649. func (mv Map) XmlWriter(xmlWriter io.Writer, rootTag ...string) error {
  650. x, err := mv.Xml(rootTag...)
  651. if err != nil {
  652. return err
  653. }
  654. _, err = xmlWriter.Write(x)
  655. return err
  656. }
  657. // Writes the Map as XML on the Writer. []byte is the raw XML that was written.
  658. // See Xml() for encoding rules.
  659. /*
  660. func (mv Map) XmlWriterRaw(xmlWriter io.Writer, rootTag ...string) ([]byte, error) {
  661. x, err := mv.Xml(rootTag...)
  662. if err != nil {
  663. return x, err
  664. }
  665. _, err = xmlWriter.Write(x)
  666. return x, err
  667. }
  668. */
  669. // Writes the Map as pretty XML on the Writer.
  670. // See Xml() for encoding rules.
  671. func (mv Map) XmlIndentWriter(xmlWriter io.Writer, prefix, indent string, rootTag ...string) error {
  672. x, err := mv.XmlIndent(prefix, indent, rootTag...)
  673. if err != nil {
  674. return err
  675. }
  676. _, err = xmlWriter.Write(x)
  677. return err
  678. }
  679. // Writes the Map as pretty XML on the Writer. []byte is the raw XML that was written.
  680. // See Xml() for encoding rules.
  681. /*
  682. func (mv Map) XmlIndentWriterRaw(xmlWriter io.Writer, prefix, indent string, rootTag ...string) ([]byte, error) {
  683. x, err := mv.XmlIndent(prefix, indent, rootTag...)
  684. if err != nil {
  685. return x, err
  686. }
  687. _, err = xmlWriter.Write(x)
  688. return x, err
  689. }
  690. */
  691. // -------------------- END: mv.Xml & mv.XmlWriter -------------------------------
  692. // -------------- Handle XML stream by processing Map value --------------------
  693. // Default poll delay to keep Handler from spinning on an open stream
  694. // like sitting on os.Stdin waiting for imput.
  695. var xhandlerPollInterval = time.Millisecond
  696. // Bulk process XML using handlers that process a Map value.
  697. // 'rdr' is an io.Reader for XML (stream)
  698. // 'mapHandler' is the Map processor. Return of 'false' stops io.Reader processing.
  699. // 'errHandler' is the error processor. Return of 'false' stops io.Reader processing and returns the error.
  700. // Note: mapHandler() and errHandler() calls are blocking, so reading and processing of messages is serialized.
  701. // This means that you can stop reading the file on error or after processing a particular message.
  702. // To have reading and handling run concurrently, pass argument to a go routine in handler and return 'true'.
  703. func HandleXmlReader(xmlReader io.Reader, mapHandler func(Map) bool, errHandler func(error) bool) error {
  704. var n int
  705. for {
  706. m, merr := NewMapXmlReader(xmlReader)
  707. n++
  708. // handle error condition with errhandler
  709. if merr != nil && merr != io.EOF {
  710. merr = fmt.Errorf("[xmlReader: %d] %s", n, merr.Error())
  711. if ok := errHandler(merr); !ok {
  712. // caused reader termination
  713. return merr
  714. }
  715. continue
  716. }
  717. // pass to maphandler
  718. if len(m) != 0 {
  719. if ok := mapHandler(m); !ok {
  720. break
  721. }
  722. } else if merr != io.EOF {
  723. time.Sleep(xhandlerPollInterval)
  724. }
  725. if merr == io.EOF {
  726. break
  727. }
  728. }
  729. return nil
  730. }
  731. // Bulk process XML using handlers that process a Map value and the raw XML.
  732. // 'rdr' is an io.Reader for XML (stream)
  733. // 'mapHandler' is the Map and raw XML - []byte - processor. Return of 'false' stops io.Reader processing.
  734. // 'errHandler' is the error and raw XML processor. Return of 'false' stops io.Reader processing and returns the error.
  735. // Note: mapHandler() and errHandler() calls are blocking, so reading and processing of messages is serialized.
  736. // This means that you can stop reading the file on error or after processing a particular message.
  737. // To have reading and handling run concurrently, pass argument(s) to a go routine in handler and return 'true'.
  738. // See NewMapXmlReaderRaw for comment on performance associated with retrieving raw XML from a Reader.
  739. func HandleXmlReaderRaw(xmlReader io.Reader, mapHandler func(Map, []byte) bool, errHandler func(error, []byte) bool) error {
  740. var n int
  741. for {
  742. m, raw, merr := NewMapXmlReaderRaw(xmlReader)
  743. n++
  744. // handle error condition with errhandler
  745. if merr != nil && merr != io.EOF {
  746. merr = fmt.Errorf("[xmlReader: %d] %s", n, merr.Error())
  747. if ok := errHandler(merr, raw); !ok {
  748. // caused reader termination
  749. return merr
  750. }
  751. continue
  752. }
  753. // pass to maphandler
  754. if len(m) != 0 {
  755. if ok := mapHandler(m, raw); !ok {
  756. break
  757. }
  758. } else if merr != io.EOF {
  759. time.Sleep(xhandlerPollInterval)
  760. }
  761. if merr == io.EOF {
  762. break
  763. }
  764. }
  765. return nil
  766. }
  767. // ----------------- END: Handle XML stream by processing Map value --------------
  768. // -------- a hack of io.TeeReader ... need one that's an io.ByteReader for xml.NewDecoder() ----------
  769. // This is a clone of io.TeeReader with the additional method t.ReadByte().
  770. // Thus, this TeeReader is also an io.ByteReader.
  771. // This is necessary because xml.NewDecoder uses a ByteReader not a Reader. It appears to have been written
  772. // with bufio.Reader or bytes.Reader in mind ... not a generic io.Reader, which doesn't have to have ReadByte()..
  773. // If NewDecoder is passed a Reader that does not satisfy ByteReader() it wraps the Reader with
  774. // bufio.NewReader and uses ReadByte rather than Read that runs the TeeReader pipe logic.
  775. type teeReader struct {
  776. r io.Reader
  777. w io.Writer
  778. b []byte
  779. }
  780. func myTeeReader(r io.Reader, w io.Writer) io.Reader {
  781. b := make([]byte, 1)
  782. return &teeReader{r, w, b}
  783. }
  784. // need for io.Reader - but we don't use it ...
  785. func (t *teeReader) Read(p []byte) (int, error) {
  786. return 0, nil
  787. }
  788. func (t *teeReader) ReadByte() (byte, error) {
  789. n, err := t.r.Read(t.b)
  790. if n > 0 {
  791. if _, err := t.w.Write(t.b[:1]); err != nil {
  792. return t.b[0], err
  793. }
  794. }
  795. return t.b[0], err
  796. }
  797. // For use with NewMapXmlReader & NewMapXmlSeqReader.
  798. type byteReader struct {
  799. r io.Reader
  800. b []byte
  801. }
  802. func myByteReader(r io.Reader) io.Reader {
  803. b := make([]byte, 1)
  804. return &byteReader{r, b}
  805. }
  806. // Need for io.Reader interface ...
  807. // Needed if reading a malformed http.Request.Body - issue #38.
  808. func (b *byteReader) Read(p []byte) (int, error) {
  809. return b.r.Read(p)
  810. }
  811. func (b *byteReader) ReadByte() (byte, error) {
  812. _, err := b.r.Read(b.b)
  813. if len(b.b) > 0 {
  814. return b.b[0], nil
  815. }
  816. var c byte
  817. return c, err
  818. }
  819. // ----------------------- END: io.TeeReader hack -----------------------------------
  820. // ---------------------- XmlIndent - from j2x package ----------------------------
  821. // Encode a map[string]interface{} as a pretty XML string.
  822. // See Xml for encoding rules.
  823. func (mv Map) XmlIndent(prefix, indent string, rootTag ...string) ([]byte, error) {
  824. m := map[string]interface{}(mv)
  825. var err error
  826. b := new(bytes.Buffer)
  827. p := new(pretty)
  828. p.indent = indent
  829. p.padding = prefix
  830. if len(m) == 1 && len(rootTag) == 0 {
  831. // this can extract the key for the single map element
  832. // use it if it isn't a key for a list
  833. for key, value := range m {
  834. if _, ok := value.([]interface{}); ok {
  835. err = marshalMapToXmlIndent(true, b, DefaultRootTag, m, p)
  836. } else {
  837. err = marshalMapToXmlIndent(true, b, key, value, p)
  838. }
  839. }
  840. } else if len(rootTag) == 1 {
  841. err = marshalMapToXmlIndent(true, b, rootTag[0], m, p)
  842. } else {
  843. err = marshalMapToXmlIndent(true, b, DefaultRootTag, m, p)
  844. }
  845. if xmlCheckIsValid {
  846. d := xml.NewDecoder(bytes.NewReader(b.Bytes()))
  847. for {
  848. _, err = d.Token()
  849. if err == io.EOF {
  850. err = nil
  851. break
  852. } else if err != nil {
  853. return nil, err
  854. }
  855. }
  856. }
  857. return b.Bytes(), err
  858. }
  859. type pretty struct {
  860. indent string
  861. cnt int
  862. padding string
  863. mapDepth int
  864. start int
  865. }
  866. func (p *pretty) Indent() {
  867. p.padding += p.indent
  868. p.cnt++
  869. }
  870. func (p *pretty) Outdent() {
  871. if p.cnt > 0 {
  872. p.padding = p.padding[:len(p.padding)-len(p.indent)]
  873. p.cnt--
  874. }
  875. }
  876. // where the work actually happens
  877. // returns an error if an attribute is not atomic
  878. // NOTE: 01may20 - replaces mapToXmlIndent(); uses bytes.Buffer instead for string appends.
  879. func marshalMapToXmlIndent(doIndent bool, b *bytes.Buffer, key string, value interface{}, pp *pretty) error {
  880. var err error
  881. var endTag bool
  882. var isSimple bool
  883. var elen int
  884. p := &pretty{pp.indent, pp.cnt, pp.padding, pp.mapDepth, pp.start}
  885. // per issue #48, 18apr18 - try and coerce maps to map[string]interface{}
  886. // Don't need for mapToXmlSeqIndent, since maps there are decoded by NewMapXmlSeq().
  887. if reflect.ValueOf(value).Kind() == reflect.Map {
  888. switch value.(type) {
  889. case map[string]interface{}:
  890. default:
  891. val := make(map[string]interface{})
  892. vv := reflect.ValueOf(value)
  893. keys := vv.MapKeys()
  894. for _, k := range keys {
  895. val[fmt.Sprint(k)] = vv.MapIndex(k).Interface()
  896. }
  897. value = val
  898. }
  899. }
  900. // 14jul20. The following block of code has become something of a catch all for odd stuff
  901. // that might be passed in as a result of casting an arbitrary map[<T>]<T> to an mxj.Map
  902. // value and then call m.Xml or m.XmlIndent. See issue #71 (and #73) for such edge cases.
  903. switch value.(type) {
  904. // these types are handled during encoding
  905. case map[string]interface{}, []byte, string, float64, bool, int, int32, int64, float32, json.Number:
  906. case []map[string]interface{}, []string, []float64, []bool, []int, []int32, []int64, []float32, []json.Number:
  907. case []interface{}:
  908. case nil:
  909. value = ""
  910. default:
  911. // see if value is a struct, if so marshal using encoding/xml package
  912. if reflect.ValueOf(value).Kind() == reflect.Struct {
  913. if v, err := xml.Marshal(value); err != nil {
  914. return err
  915. } else {
  916. value = string(v)
  917. }
  918. } else {
  919. // coerce eveything else into a string value
  920. value = fmt.Sprint(value)
  921. }
  922. }
  923. // start the XML tag with required indentaton and padding
  924. if doIndent {
  925. if _, err = b.WriteString(p.padding); err != nil {
  926. return err
  927. }
  928. }
  929. switch value.(type) {
  930. case []interface{}:
  931. default:
  932. if _, err = b.WriteString(`<` + key); err != nil {
  933. return err
  934. }
  935. }
  936. switch value.(type) {
  937. case map[string]interface{}:
  938. vv := value.(map[string]interface{})
  939. lenvv := len(vv)
  940. // scan out attributes - attribute keys have prepended attrPrefix
  941. attrlist := make([][2]string, len(vv))
  942. var n int
  943. var ss string
  944. for k, v := range vv {
  945. if lenAttrPrefix > 0 && lenAttrPrefix < len(k) && k[:lenAttrPrefix] == attrPrefix {
  946. switch v.(type) {
  947. case string:
  948. if xmlEscapeChars {
  949. ss = escapeChars(v.(string))
  950. } else {
  951. ss = v.(string)
  952. }
  953. attrlist[n][0] = k[lenAttrPrefix:]
  954. attrlist[n][1] = ss
  955. case float64, bool, int, int32, int64, float32, json.Number:
  956. attrlist[n][0] = k[lenAttrPrefix:]
  957. attrlist[n][1] = fmt.Sprintf("%v", v)
  958. case []byte:
  959. if xmlEscapeChars {
  960. ss = escapeChars(string(v.([]byte)))
  961. } else {
  962. ss = string(v.([]byte))
  963. }
  964. attrlist[n][0] = k[lenAttrPrefix:]
  965. attrlist[n][1] = ss
  966. default:
  967. return fmt.Errorf("invalid attribute value for: %s:<%T>", k, v)
  968. }
  969. n++
  970. }
  971. }
  972. if n > 0 {
  973. attrlist = attrlist[:n]
  974. sort.Sort(attrList(attrlist))
  975. for _, v := range attrlist {
  976. if _, err = b.WriteString(` ` + v[0] + `="` + v[1] + `"`); err != nil {
  977. return err
  978. }
  979. }
  980. }
  981. // only attributes?
  982. if n == lenvv {
  983. if useGoXmlEmptyElemSyntax {
  984. if _, err = b.WriteString(`</` + key + ">"); err != nil {
  985. return err
  986. }
  987. } else {
  988. if _, err = b.WriteString(`/>`); err != nil {
  989. return err
  990. }
  991. }
  992. break
  993. }
  994. // simple element? Note: '#text" is an invalid XML tag.
  995. isComplex := false
  996. if v, ok := vv["#text"]; ok && n+1 == lenvv {
  997. // just the value and attributes
  998. switch v.(type) {
  999. case string:
  1000. if xmlEscapeChars {
  1001. v = escapeChars(v.(string))
  1002. } else {
  1003. v = v.(string)
  1004. }
  1005. case []byte:
  1006. if xmlEscapeChars {
  1007. v = escapeChars(string(v.([]byte)))
  1008. } else {
  1009. v = string(v.([]byte))
  1010. }
  1011. }
  1012. if _, err = b.WriteString(">" + fmt.Sprintf("%v", v)); err != nil {
  1013. return err
  1014. }
  1015. endTag = true
  1016. elen = 1
  1017. isSimple = true
  1018. break
  1019. } else if ok {
  1020. // need to handle when there are subelements in addition to the simple element value
  1021. // issue #90
  1022. switch v.(type) {
  1023. case string:
  1024. if xmlEscapeChars {
  1025. v = escapeChars(v.(string))
  1026. } else {
  1027. v = v.(string)
  1028. }
  1029. case []byte:
  1030. if xmlEscapeChars {
  1031. v = escapeChars(string(v.([]byte)))
  1032. } else {
  1033. v = string(v.([]byte))
  1034. }
  1035. }
  1036. if _, err = b.WriteString(">" + fmt.Sprintf("%v", v)); err != nil {
  1037. return err
  1038. }
  1039. isComplex = true
  1040. }
  1041. // close tag with possible attributes
  1042. if !isComplex {
  1043. if _, err = b.WriteString(">"); err != nil {
  1044. return err
  1045. }
  1046. }
  1047. if doIndent {
  1048. // *s += "\n"
  1049. if _, err = b.WriteString("\n"); err != nil {
  1050. return err
  1051. }
  1052. }
  1053. // something more complex
  1054. p.mapDepth++
  1055. // extract the map k:v pairs and sort on key
  1056. elemlist := make([][2]interface{}, len(vv))
  1057. n = 0
  1058. for k, v := range vv {
  1059. if k == "#text" {
  1060. // simple element handled above
  1061. continue
  1062. }
  1063. if lenAttrPrefix > 0 && lenAttrPrefix < len(k) && k[:lenAttrPrefix] == attrPrefix {
  1064. continue
  1065. }
  1066. elemlist[n][0] = k
  1067. elemlist[n][1] = v
  1068. n++
  1069. }
  1070. elemlist = elemlist[:n]
  1071. sort.Sort(elemList(elemlist))
  1072. var i int
  1073. for _, v := range elemlist {
  1074. switch v[1].(type) {
  1075. case []interface{}:
  1076. default:
  1077. if i == 0 && doIndent {
  1078. p.Indent()
  1079. }
  1080. }
  1081. i++
  1082. if err := marshalMapToXmlIndent(doIndent, b, v[0].(string), v[1], p); err != nil {
  1083. return err
  1084. }
  1085. switch v[1].(type) {
  1086. case []interface{}: // handled in []interface{} case
  1087. default:
  1088. if doIndent {
  1089. p.Outdent()
  1090. }
  1091. }
  1092. i--
  1093. }
  1094. p.mapDepth--
  1095. endTag = true
  1096. elen = 1 // we do have some content ...
  1097. case []interface{}:
  1098. // special case - found during implementing Issue #23
  1099. if len(value.([]interface{})) == 0 {
  1100. if doIndent {
  1101. if _, err = b.WriteString(p.padding + p.indent); err != nil {
  1102. return err
  1103. }
  1104. }
  1105. if _, err = b.WriteString("<" + key); err != nil {
  1106. return err
  1107. }
  1108. elen = 0
  1109. endTag = true
  1110. break
  1111. }
  1112. for _, v := range value.([]interface{}) {
  1113. if doIndent {
  1114. p.Indent()
  1115. }
  1116. if err := marshalMapToXmlIndent(doIndent, b, key, v, p); err != nil {
  1117. return err
  1118. }
  1119. if doIndent {
  1120. p.Outdent()
  1121. }
  1122. }
  1123. return nil
  1124. case []string:
  1125. // This was added by https://github.com/slotix ... not a type that
  1126. // would be encountered if mv generated from NewMapXml, NewMapJson.
  1127. // Could be encountered in AnyXml(), so we'll let it stay, though
  1128. // it should be merged with case []interface{}, above.
  1129. //quick fix for []string type
  1130. //[]string should be treated exaclty as []interface{}
  1131. if len(value.([]string)) == 0 {
  1132. if doIndent {
  1133. if _, err = b.WriteString(p.padding + p.indent); err != nil {
  1134. return err
  1135. }
  1136. }
  1137. if _, err = b.WriteString("<" + key); err != nil {
  1138. return err
  1139. }
  1140. elen = 0
  1141. endTag = true
  1142. break
  1143. }
  1144. for _, v := range value.([]string) {
  1145. if doIndent {
  1146. p.Indent()
  1147. }
  1148. if err := marshalMapToXmlIndent(doIndent, b, key, v, p); err != nil {
  1149. return err
  1150. }
  1151. if doIndent {
  1152. p.Outdent()
  1153. }
  1154. }
  1155. return nil
  1156. case nil:
  1157. // terminate the tag
  1158. if doIndent {
  1159. // *s += p.padding
  1160. if _, err = b.WriteString(p.padding); err != nil {
  1161. return err
  1162. }
  1163. }
  1164. if _, err = b.WriteString("<" + key); err != nil {
  1165. return err
  1166. }
  1167. endTag, isSimple = true, true
  1168. break
  1169. default: // handle anything - even goofy stuff
  1170. elen = 0
  1171. switch value.(type) {
  1172. case string:
  1173. v := value.(string)
  1174. if xmlEscapeChars {
  1175. v = escapeChars(v)
  1176. }
  1177. elen = len(v)
  1178. if elen > 0 {
  1179. // *s += ">" + v
  1180. if _, err = b.WriteString(">" + v); err != nil {
  1181. return err
  1182. }
  1183. }
  1184. case float64, bool, int, int32, int64, float32, json.Number:
  1185. v := fmt.Sprintf("%v", value)
  1186. elen = len(v) // always > 0
  1187. if _, err = b.WriteString(">" + v); err != nil {
  1188. return err
  1189. }
  1190. case []byte: // NOTE: byte is just an alias for uint8
  1191. // similar to how xml.Marshal handles []byte structure members
  1192. v := string(value.([]byte))
  1193. if xmlEscapeChars {
  1194. v = escapeChars(v)
  1195. }
  1196. elen = len(v)
  1197. if elen > 0 {
  1198. // *s += ">" + v
  1199. if _, err = b.WriteString(">" + v); err != nil {
  1200. return err
  1201. }
  1202. }
  1203. default:
  1204. if _, err = b.WriteString(">"); err != nil {
  1205. return err
  1206. }
  1207. var v []byte
  1208. var err error
  1209. if doIndent {
  1210. v, err = xml.MarshalIndent(value, p.padding, p.indent)
  1211. } else {
  1212. v, err = xml.Marshal(value)
  1213. }
  1214. if err != nil {
  1215. if _, err = b.WriteString(">UNKNOWN"); err != nil {
  1216. return err
  1217. }
  1218. } else {
  1219. elen = len(v)
  1220. if elen > 0 {
  1221. if _, err = b.Write(v); err != nil {
  1222. return err
  1223. }
  1224. }
  1225. }
  1226. }
  1227. isSimple = true
  1228. endTag = true
  1229. }
  1230. if endTag {
  1231. if doIndent {
  1232. if !isSimple {
  1233. if _, err = b.WriteString(p.padding); err != nil {
  1234. return err
  1235. }
  1236. }
  1237. }
  1238. if elen > 0 || useGoXmlEmptyElemSyntax {
  1239. if elen == 0 {
  1240. if _, err = b.WriteString(">"); err != nil {
  1241. return err
  1242. }
  1243. }
  1244. if _, err = b.WriteString(`</` + key + ">"); err != nil {
  1245. return err
  1246. }
  1247. } else {
  1248. if _, err = b.WriteString(`/>`); err != nil {
  1249. return err
  1250. }
  1251. }
  1252. }
  1253. if doIndent {
  1254. if p.cnt > p.start {
  1255. if _, err = b.WriteString("\n"); err != nil {
  1256. return err
  1257. }
  1258. }
  1259. p.Outdent()
  1260. }
  1261. return nil
  1262. }
  1263. // ============================ sort interface implementation =================
  1264. type attrList [][2]string
  1265. func (a attrList) Len() int {
  1266. return len(a)
  1267. }
  1268. func (a attrList) Swap(i, j int) {
  1269. a[i], a[j] = a[j], a[i]
  1270. }
  1271. func (a attrList) Less(i, j int) bool {
  1272. return a[i][0] <= a[j][0]
  1273. }
  1274. type elemList [][2]interface{}
  1275. func (e elemList) Len() int {
  1276. return len(e)
  1277. }
  1278. func (e elemList) Swap(i, j int) {
  1279. e[i], e[j] = e[j], e[i]
  1280. }
  1281. func (e elemList) Less(i, j int) bool {
  1282. return e[i][0].(string) <= e[j][0].(string)
  1283. }