2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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 (
|
2019-03-30 23:39:07 +08:00
|
|
|
"fmt"
|
2018-06-07 08:37:24 +08:00
|
|
|
"strings"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2019-08-01 14:07:25 +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
|
|
|
)
|
|
|
|
|
2018-06-01 20:52:17 +08:00
|
|
|
// 将XML内容解析为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
|
|
|
|
}
|
|
|
|
return mxj.NewMapXml(res)
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 将map变量解析为XML格式内容
|
2019-03-30 23:39:07 +08:00
|
|
|
func Encode(v map[string]interface{}, rootTag ...string) ([]byte, error) {
|
|
|
|
return mxj.Map(v).Xml(rootTag...)
|
2018-01-19 16:19:48 +08:00
|
|
|
}
|
|
|
|
|
2019-03-30 23:39:07 +08:00
|
|
|
func EncodeWithIndent(v map[string]interface{}, rootTag ...string) ([]byte, error) {
|
|
|
|
return mxj.Map(v).XmlIndent("", "\t", rootTag...)
|
2018-01-19 15:26:28 +08:00
|
|
|
}
|
|
|
|
|
2018-06-01 20:52:17 +08:00
|
|
|
// XML格式内容直接转换为JSON格式内容
|
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 {
|
|
|
|
fmt.Println("convert error. ", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mv, err := mxj.NewMapXml(res)
|
2018-06-01 20:52:17 +08:00
|
|
|
if err == nil {
|
2019-03-30 23:39:07 +08:00
|
|
|
return mv.Json()
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-04 12:32:32 +08:00
|
|
|
}
|
|
|
|
|
2018-06-07 08:37:24 +08:00
|
|
|
// XML字符集预处理
|
2019-06-01 00:02:05 +08:00
|
|
|
func convert(xml []byte) (res []byte, err error) {
|
2018-06-07 08:37:24 +08:00
|
|
|
patten := `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
|
2019-06-01 00:02:05 +08:00
|
|
|
matchStr, err := gregex.MatchString(patten, string(xml))
|
2018-06-04 12:32:32 +08:00
|
|
|
if err != nil {
|
2019-03-30 23:39:07 +08:00
|
|
|
return nil, err
|
2018-06-04 12:32:32 +08:00
|
|
|
}
|
2018-07-04 11:17:15 +08:00
|
|
|
xmlEncode := "UTF-8"
|
|
|
|
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
|
|
|
|
}
|