gxml.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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"
  11. "github.com/gogf/gf/encoding/gcharset"
  12. "github.com/gogf/gf/text/gregex"
  13. )
  14. // Decode parses <content> into and returns as map.
  15. func Decode(content []byte) (map[string]interface{}, error) {
  16. res, err := convert(content)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return mxj.NewMapXml(res)
  21. }
  22. // DecodeWithoutRoot parses <content> into a map, and returns the map without root level.
  23. func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
  24. res, err := convert(content)
  25. if err != nil {
  26. return nil, err
  27. }
  28. m, err := mxj.NewMapXml(res)
  29. if err != nil {
  30. return nil, err
  31. }
  32. for _, v := range m {
  33. if r, ok := v.(map[string]interface{}); ok {
  34. return r, nil
  35. }
  36. }
  37. return m, nil
  38. }
  39. // Encode encodes map <m> to a XML format content as bytes.
  40. // The optional parameter <rootTag> is used to specify the XML root tag.
  41. func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
  42. return mxj.Map(m).Xml(rootTag...)
  43. }
  44. // Encode encodes map <m> to a XML format content as bytes with indent.
  45. // The optional parameter <rootTag> is used to specify the XML root tag.
  46. func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
  47. return mxj.Map(m).XmlIndent("", "\t", rootTag...)
  48. }
  49. // ToJson converts <content> as XML format into JSON format bytes.
  50. func ToJson(content []byte) ([]byte, error) {
  51. res, err := convert(content)
  52. if err != nil {
  53. return nil, err
  54. }
  55. mv, err := mxj.NewMapXml(res)
  56. if err == nil {
  57. return mv.Json()
  58. } else {
  59. return nil, err
  60. }
  61. }
  62. // convert converts the encoding of given XML content from XML root tag into UTF-8 encoding content.
  63. func convert(xml []byte) (res []byte, err error) {
  64. patten := `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
  65. matchStr, err := gregex.MatchString(patten, string(xml))
  66. if err != nil {
  67. return nil, err
  68. }
  69. xmlEncode := "UTF-8"
  70. if len(matchStr) == 2 {
  71. xmlEncode = matchStr[1]
  72. }
  73. xmlEncode = strings.ToUpper(xmlEncode)
  74. res, err = gregex.Replace(patten, []byte(""), xml)
  75. if err != nil {
  76. return nil, err
  77. }
  78. if xmlEncode != "UTF-8" && xmlEncode != "UTF8" {
  79. dst, err := gcharset.Convert("UTF-8", xmlEncode, string(res))
  80. if err != nil {
  81. return nil, err
  82. }
  83. res = []byte(dst)
  84. }
  85. return res, nil
  86. }