doc.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // mxj - A collection of map[string]interface{} and associated XML and JSON utilities.
  2. // Copyright 2012-2019, Charles Banning. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file
  5. /*
  6. Marshal/Unmarshal XML to/from map[string]interface{} values (and JSON); extract/modify values from maps by key or key-path, including wildcards.
  7. mxj supplants the legacy x2j and j2x packages. The subpackage x2j-wrapper is provided to facilitate migrating from the x2j package. The x2j and j2x subpackages provide similar functionality of the old packages but are not function-name compatible with them.
  8. Note: this library was designed for processing ad hoc anonymous messages. Bulk processing large data sets may be much more efficiently performed using the encoding/xml or encoding/json packages from Go's standard library directly.
  9. Related Packages:
  10. checkxml: github.com/clbanning/checkxml provides functions for validating XML data.
  11. Notes:
  12. 2020.05.01: v2.2 - optimize map to XML encoding for large XML docs.
  13. 2019.07.04: v2.0 - remove unnecessary methods - mv.XmlWriterRaw, mv.XmlIndentWriterRaw - for Map and MapSeq.
  14. 2019.07.04: Add MapSeq type and move associated functions and methods from Map to MapSeq.
  15. 2019.01.21: DecodeSimpleValuesAsMap - decode to map[<tag>:map["#text":<value>]] rather than map[<tag>:<value>].
  16. 2018.04.18: mv.Xml/mv.XmlIndent encodes non-map[string]interface{} map values - map[string]string, map[int]uint, etc.
  17. 2018.03.29: mv.Gob/NewMapGob support gob encoding/decoding of Maps.
  18. 2018.03.26: Added mxj/x2j-wrapper sub-package for migrating from legacy x2j package.
  19. 2017.02.22: LeafNode paths can use ".N" syntax rather than "[N]" for list member indexing.
  20. 2017.02.21: github.com/clbanning/checkxml provides functions for validating XML data.
  21. 2017.02.10: SetFieldSeparator changes field separator for args in UpdateValuesForPath, ValuesFor... methods.
  22. 2017.02.06: Support XMPP stream processing - HandleXMPPStreamTag().
  23. 2016.11.07: Preserve name space prefix syntax in XmlSeq parser - NewMapXmlSeq(), etc.
  24. 2016.06.25: Support overriding default XML attribute prefix, "-", in Map keys - SetAttrPrefix().
  25. 2016.05.26: Support customization of xml.Decoder by exposing CustomDecoder variable.
  26. 2016.03.19: Escape invalid chars when encoding XML attribute and element values - XMLEscapeChars().
  27. 2016.03.02: By default decoding XML with float64 and bool value casting will not cast "NaN", "Inf", and "-Inf".
  28. To cast them to float64, first set flag with CastNanInf(true).
  29. 2016.02.22: New mv.Root(), mv.Elements(), mv.Attributes methods let you examine XML document structure.
  30. 2016.02.16: Add CoerceKeysToLower() option to handle tags with mixed capitalization.
  31. 2016.02.12: Seek for first xml.StartElement token; only return error if io.EOF is reached first (handles BOM).
  32. 2015-12-02: NewMapXmlSeq() with mv.XmlSeq() & co. will try to preserve structure of XML doc when re-encoding.
  33. 2014-08-02: AnyXml() and AnyXmlIndent() will try to marshal arbitrary values to XML.
  34. SUMMARY
  35. type Map map[string]interface{}
  36. Create a Map value, 'mv', from any map[string]interface{} value, 'v':
  37. mv := Map(v)
  38. Unmarshal / marshal XML as a Map value, 'mv':
  39. mv, err := NewMapXml(xmlValue) // unmarshal
  40. xmlValue, err := mv.Xml() // marshal
  41. Unmarshal XML from an io.Reader as a Map value, 'mv':
  42. mv, err := NewMapXmlReader(xmlReader) // repeated calls, as with an os.File Reader, will process stream
  43. mv, raw, err := NewMapXmlReaderRaw(xmlReader) // 'raw' is the raw XML that was decoded
  44. Marshal Map value, 'mv', to an XML Writer (io.Writer):
  45. err := mv.XmlWriter(xmlWriter)
  46. raw, err := mv.XmlWriterRaw(xmlWriter) // 'raw' is the raw XML that was written on xmlWriter
  47. Also, for prettified output:
  48. xmlValue, err := mv.XmlIndent(prefix, indent, ...)
  49. err := mv.XmlIndentWriter(xmlWriter, prefix, indent, ...)
  50. raw, err := mv.XmlIndentWriterRaw(xmlWriter, prefix, indent, ...)
  51. Bulk process XML with error handling (note: handlers must return a boolean value):
  52. err := HandleXmlReader(xmlReader, mapHandler(Map), errHandler(error))
  53. err := HandleXmlReaderRaw(xmlReader, mapHandler(Map, []byte), errHandler(error, []byte))
  54. Converting XML to JSON: see Examples for NewMapXml and HandleXmlReader.
  55. There are comparable functions and methods for JSON processing.
  56. Arbitrary structure values can be decoded to / encoded from Map values:
  57. mv, err := NewMapStruct(structVal)
  58. err := mv.Struct(structPointer)
  59. To work with XML tag values, JSON or Map key values or structure field values, decode the XML, JSON
  60. or structure to a Map value, 'mv', or cast a map[string]interface{} value to a Map value, 'mv', then:
  61. paths := mv.PathsForKey(key)
  62. path := mv.PathForKeyShortest(key)
  63. values, err := mv.ValuesForKey(key, subkeys)
  64. values, err := mv.ValuesForPath(path, subkeys) // 'path' can be dot-notation with wildcards and indexed arrays.
  65. count, err := mv.UpdateValuesForPath(newVal, path, subkeys)
  66. Get everything at once, irrespective of path depth:
  67. leafnodes := mv.LeafNodes()
  68. leafvalues := mv.LeafValues()
  69. A new Map with whatever keys are desired can be created from the current Map and then encoded in XML
  70. or JSON. (Note: keys can use dot-notation. 'oldKey' can also use wildcards and indexed arrays.)
  71. newMap, err := mv.NewMap("oldKey_1:newKey_1", "oldKey_2:newKey_2", ..., "oldKey_N:newKey_N")
  72. newMap, err := mv.NewMap("oldKey1", "oldKey3", "oldKey5") // a subset of 'mv'; see "examples/partial.go"
  73. newXml, err := newMap.Xml() // for example
  74. newJson, err := newMap.Json() // ditto
  75. XML PARSING CONVENTIONS
  76. Using NewMapXml()
  77. - Attributes are parsed to `map[string]interface{}` values by prefixing a hyphen, `-`,
  78. to the attribute label. (Unless overridden by `PrependAttrWithHyphen(false)` or
  79. `SetAttrPrefix()`.)
  80. - If the element is a simple element and has attributes, the element value
  81. is given the key `#text` for its `map[string]interface{}` representation. (See
  82. the 'atomFeedString.xml' test data, below.)
  83. - XML comments, directives, and process instructions are ignored.
  84. - If CoerceKeysToLower() has been called, then the resultant keys will be lower case.
  85. Using NewMapXmlSeq()
  86. - Attributes are parsed to `map["#attr"]map[<attr_label>]map[string]interface{}`values
  87. where the `<attr_label>` value has "#text" and "#seq" keys - the "#text" key holds the
  88. value for `<attr_label>`.
  89. - All elements, except for the root, have a "#seq" key.
  90. - Comments, directives, and process instructions are unmarshalled into the Map using the
  91. keys "#comment", "#directive", and "#procinst", respectively. (See documentation for more
  92. specifics.)
  93. - Name space syntax is preserved:
  94. - <ns:key>something</ns.key> parses to map["ns:key"]interface{}{"something"}
  95. - xmlns:ns="http://myns.com/ns" parses to map["xmlns:ns"]interface{}{"http://myns.com/ns"}
  96. Both
  97. - By default, "Nan", "Inf", and "-Inf" values are not cast to float64. If you want them
  98. to be cast, set a flag to cast them using CastNanInf(true).
  99. XML ENCODING CONVENTIONS
  100. - 'nil' Map values, which may represent 'null' JSON values, are encoded as "<tag/>".
  101. NOTE: the operation is not symmetric as "<tag/>" elements are decoded as 'tag:""' Map values,
  102. which, then, encode in JSON as '"tag":""' values..
  103. - ALSO: there is no guarantee that the encoded XML doc will be the same as the decoded one. (Go
  104. randomizes the walk through map[string]interface{} values.) If you plan to re-encode the
  105. Map value to XML and want the same sequencing of elements look at NewMapXmlSeq() and
  106. mv.XmlSeq() - these try to preserve the element sequencing but with added complexity when
  107. working with the Map representation.
  108. */
  109. package mxj