gf/encoding/gxml/gxml.go

96 lines
2.5 KiB
Go
Raw Normal View History

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,
// 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 (
"strings"
2019-07-29 21:01:19 +08:00
"github.com/clbanning/mxj"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/encoding/gcharset"
"github.com/gogf/gf/text/gregex"
2018-01-19 15:26:28 +08:00
)
// Decode parses <content> into and returns as map.
func Decode(content []byte) (map[string]interface{}, error) {
res, err := convert(content)
if err != nil {
return nil, err
}
return mxj.NewMapXml(res)
2018-01-19 15:26:28 +08:00
}
// DecodeWithoutRoot parses <content> into a map, and returns the map without root level.
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 {
return nil, err
}
for _, v := range m {
if r, ok := v.(map[string]interface{}); ok {
return r, nil
}
}
return m, nil
}
// Encode encodes map <m> to a XML format content as bytes.
// The optional parameter <rootTag> is used to specify the XML root tag.
func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
return mxj.Map(m).Xml(rootTag...)
2018-01-19 15:26:28 +08:00
}
// Encode encodes map <m> to a XML format content as bytes with indent.
// The optional parameter <rootTag> is used to specify the XML root tag.
func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
return mxj.Map(m).XmlIndent("", "\t", rootTag...)
}
// ToJson converts <content> as XML format into JSON format bytes.
func ToJson(content []byte) ([]byte, error) {
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 {
return mv.Json()
} else {
return nil, err
}
}
// convert converts the encoding of given XML content from XML root tag into UTF-8 encoding content.
func convert(xml []byte) (res []byte, err error) {
patten := `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
matchStr, err := gregex.MatchString(patten, string(xml))
if err != nil {
return nil, err
}
xmlEncode := "UTF-8"
if len(matchStr) == 2 {
xmlEncode = matchStr[1]
}
xmlEncode = strings.ToUpper(xmlEncode)
2019-06-19 09:06:52 +08:00
res, err = gregex.Replace(patten, []byte(""), xml)
if err != nil {
return nil, err
}
if xmlEncode != "UTF-8" && xmlEncode != "UTF8" {
dst, err := gcharset.Convert("UTF-8", xmlEncode, string(res))
if err != nil {
return nil, err
}
res = []byte(dst)
}
return res, nil
}