2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-01-19 15:26:28 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-01-19 15:26:28 +08:00
|
|
|
|
2019-01-15 23:27:47 +08:00
|
|
|
// Package gxml provides accessing and converting for XML content.
|
2018-01-19 15:26:28 +08:00
|
|
|
package gxml
|
|
|
|
|
|
|
|
import (
|
2018-06-07 08:37:24 +08:00
|
|
|
"strings"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-09-22 19:43:52 +08:00
|
|
|
"github.com/clbanning/mxj/v2"
|
2021-12-21 22:59:14 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-11-16 19:43:02 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/encoding/gcharset"
|
|
|
|
"github.com/gogf/gf/v2/text/gregex"
|
2018-01-19 15:26:28 +08:00
|
|
|
)
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// Decode parses `content` into and returns as map.
|
2019-03-28 17:51:05 +08:00
|
|
|
func Decode(content []byte) (map[string]interface{}, error) {
|
2019-03-30 23:39:07 +08:00
|
|
|
res, err := convert(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-21 22:59:14 +08:00
|
|
|
m, err := mxj.NewMapXml(res)
|
|
|
|
if err != nil {
|
|
|
|
err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
|
|
|
|
}
|
|
|
|
return m, err
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// DecodeWithoutRoot parses `content` into a map, and returns the map without root level.
|
2020-01-01 14:18:00 +08:00
|
|
|
func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
|
|
|
|
res, err := convert(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
m, err := mxj.NewMapXml(res)
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
|
2020-01-01 14:18:00 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, v := range m {
|
|
|
|
if r, ok := v.(map[string]interface{}); ok {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m, nil
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// Encode encodes map `m` to an XML format content as bytes.
|
|
|
|
// The optional parameter `rootTag` is used to specify the XML root tag.
|
2020-01-01 14:18:00 +08:00
|
|
|
func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
|
2021-12-21 22:59:14 +08:00
|
|
|
b, err := mxj.Map(m).Xml(rootTag...)
|
|
|
|
if err != nil {
|
|
|
|
err = gerror.Wrapf(err, `mxj.Map.Xml failed`)
|
|
|
|
}
|
|
|
|
return b, err
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// EncodeWithIndent encodes map `m` to an XML format content as bytes with indent.
|
|
|
|
// The optional parameter `rootTag` is used to specify the XML root tag.
|
2020-01-01 14:18:00 +08:00
|
|
|
func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
|
2021-12-21 22:59:14 +08:00
|
|
|
b, err := mxj.Map(m).XmlIndent("", "\t", rootTag...)
|
|
|
|
if err != nil {
|
|
|
|
err = gerror.Wrapf(err, `mxj.Map.XmlIndent failed`)
|
|
|
|
}
|
|
|
|
return b, err
|
2020-01-01 14:18:00 +08:00
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// ToJson converts `content` as XML format into JSON format bytes.
|
2019-03-28 17:51:05 +08:00
|
|
|
func ToJson(content []byte) ([]byte, error) {
|
2019-03-30 23:39:07 +08:00
|
|
|
res, err := convert(content)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mv, err := mxj.NewMapXml(res)
|
2018-06-01 20:52:17 +08:00
|
|
|
if err == nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
|
2019-03-30 23:39:07 +08:00
|
|
|
return mv.Json()
|
|
|
|
}
|
2021-11-16 19:43:02 +08:00
|
|
|
return nil, err
|
2018-06-04 12:32:32 +08:00
|
|
|
}
|
|
|
|
|
2021-09-22 19:43:52 +08:00
|
|
|
// convert does convert the encoding of given XML content from XML root tag into UTF-8 encoding content.
|
2019-06-01 00:02:05 +08:00
|
|
|
func convert(xml []byte) (res []byte, err error) {
|
2021-12-21 22:59:14 +08:00
|
|
|
var (
|
|
|
|
patten = `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
|
|
|
|
matchStr, _ = gregex.MatchString(patten, string(xml))
|
|
|
|
xmlEncode = "UTF-8"
|
|
|
|
)
|
2018-07-04 11:17:15 +08:00
|
|
|
if len(matchStr) == 2 {
|
2019-03-28 17:51:05 +08:00
|
|
|
xmlEncode = matchStr[1]
|
2018-07-04 11:17:15 +08:00
|
|
|
}
|
2019-06-11 17:25:30 +08:00
|
|
|
xmlEncode = strings.ToUpper(xmlEncode)
|
2019-06-19 09:06:52 +08:00
|
|
|
res, err = gregex.Replace(patten, []byte(""), xml)
|
2019-03-30 23:39:07 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-06-04 12:32:32 +08:00
|
|
|
}
|
2019-06-11 17:25:30 +08:00
|
|
|
if xmlEncode != "UTF-8" && xmlEncode != "UTF8" {
|
2019-06-12 21:02:00 +08:00
|
|
|
dst, err := gcharset.Convert("UTF-8", xmlEncode, string(res))
|
2019-06-11 17:25:30 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = []byte(dst)
|
2018-06-04 12:32:32 +08:00
|
|
|
}
|
2019-03-30 23:39:07 +08:00
|
|
|
return res, nil
|
|
|
|
}
|