gxml.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. // Package gxml provides accessing and converting for XML content.
  7. package gxml
  8. import (
  9. "strings"
  10. "github.com/clbanning/mxj/v2"
  11. "github.com/gogf/gf/v2/encoding/gcharset"
  12. "github.com/gogf/gf/v2/errors/gerror"
  13. "github.com/gogf/gf/v2/text/gregex"
  14. )
  15. // Decode parses `content` into and returns as map.
  16. func Decode(content []byte) (map[string]interface{}, error) {
  17. res, err := convert(content)
  18. if err != nil {
  19. return nil, err
  20. }
  21. m, err := mxj.NewMapXml(res)
  22. if err != nil {
  23. err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
  24. }
  25. return m, err
  26. }
  27. // DecodeWithoutRoot parses `content` into a map, and returns the map without root level.
  28. func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
  29. res, err := convert(content)
  30. if err != nil {
  31. return nil, err
  32. }
  33. m, err := mxj.NewMapXml(res)
  34. if err != nil {
  35. err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
  36. return nil, err
  37. }
  38. for _, v := range m {
  39. if r, ok := v.(map[string]interface{}); ok {
  40. return r, nil
  41. }
  42. }
  43. return m, nil
  44. }
  45. // Encode encodes map `m` to an XML format content as bytes.
  46. // The optional parameter `rootTag` is used to specify the XML root tag.
  47. func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
  48. b, err := mxj.Map(m).Xml(rootTag...)
  49. if err != nil {
  50. err = gerror.Wrapf(err, `mxj.Map.Xml failed`)
  51. }
  52. return b, err
  53. }
  54. // EncodeWithIndent encodes map `m` to an XML format content as bytes with indent.
  55. // The optional parameter `rootTag` is used to specify the XML root tag.
  56. func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
  57. b, err := mxj.Map(m).XmlIndent("", "\t", rootTag...)
  58. if err != nil {
  59. err = gerror.Wrapf(err, `mxj.Map.XmlIndent failed`)
  60. }
  61. return b, err
  62. }
  63. // ToJson converts `content` as XML format into JSON format bytes.
  64. func ToJson(content []byte) ([]byte, error) {
  65. res, err := convert(content)
  66. if err != nil {
  67. return nil, err
  68. }
  69. mv, err := mxj.NewMapXml(res)
  70. if err == nil {
  71. return mv.Json()
  72. }
  73. err = gerror.Wrap(err, `mxj.NewMapXml failed`)
  74. return nil, err
  75. }
  76. // convert does convert the encoding of given XML content from XML root tag into UTF-8 encoding content.
  77. func convert(xml []byte) (res []byte, err error) {
  78. var (
  79. patten = `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
  80. matchStr, _ = gregex.MatchString(patten, string(xml))
  81. xmlEncode = "UTF-8"
  82. )
  83. if len(matchStr) == 2 {
  84. xmlEncode = matchStr[1]
  85. }
  86. xmlEncode = strings.ToUpper(xmlEncode)
  87. res, err = gregex.Replace(patten, []byte(""), xml)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if xmlEncode != "UTF-8" && xmlEncode != "UTF8" {
  92. dst, err := gcharset.Convert("UTF-8", xmlEncode, string(res))
  93. if err != nil {
  94. return nil, err
  95. }
  96. res = []byte(dst)
  97. }
  98. return res, nil
  99. }