Repository navigation
Expand file tree
/
Copy pathgxml.go
More file actions
130 lines (119 loc) · 3.62 KB
/
Copy pathgxml.go
File metadata and controls
130 lines (119 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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.
// Package gxml provides accessing and converting for XML content.
package gxml
import (
"strings"
"github.com/clbanning/mxj/v2"
"github.com/gogf/gf/v2/encoding/gcharset"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gregex"
)
// Decode parses `content` into and returns as map.
func Decode(content []byte) (map[string]any, error) {
res, err := convert(content)
if err != nil {
return nil, err
}
m, err := mxj.NewMapXml(res)
if err != nil {
err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
}
return m, err
}
// DecodeWithoutRoot parses `content` into a map, and returns the map without root level.
func DecodeWithoutRoot(content []byte) (map[string]any, error) {
res, err := convert(content)
if err != nil {
return nil, err
}
m, err := mxj.NewMapXml(res)
if err != nil {
err = gerror.Wrapf(err, `mxj.NewMapXml failed`)
return nil, err
}
for _, v := range m {
if r, ok := v.(map[string]any); ok {
return r, nil
}
}
return m, nil
}
// XMLEscapeChars forces escaping invalid characters in attribute and element values.
// NOTE: this is brute force with NO interrogation of '&' being escaped already; if it is
// then '&' will be re-escaped as '&'.
//
/*
The values are:
" "
' '
< <
> >
& &
*/
//
// Note: if XMLEscapeCharsDecoder(true) has been called - or the default, 'false,' value
// has been toggled to 'true' - then XMLEscapeChars(true) is ignored. If XMLEscapeChars(true)
// has already been called before XMLEscapeCharsDecoder(true), XMLEscapeChars(false) is called
// to turn escape encoding on mv.Xml, etc., to prevent double escaping ampersands, '&'.
func XMLEscapeChars(b ...bool) {
mxj.XMLEscapeChars(b...)
}
// Encode encodes map `m` to an XML format content as bytes.
// The optional parameter `rootTag` is used to specify the XML root tag.
func Encode(m map[string]any, rootTag ...string) ([]byte, error) {
b, err := mxj.Map(m).Xml(rootTag...)
if err != nil {
err = gerror.Wrapf(err, `mxj.Map.Xml failed`)
}
return b, err
}
// 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.
func EncodeWithIndent(m map[string]any, rootTag ...string) ([]byte, error) {
b, err := mxj.Map(m).XmlIndent("", "\t", rootTag...)
if err != nil {
err = gerror.Wrapf(err, `mxj.Map.XmlIndent failed`)
}
return b, err
}
// 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)
if err == nil {
return mv.Json()
}
err = gerror.Wrap(err, `mxj.NewMapXml failed`)
return nil, err
}
// convert does convert the encoding of given XML content from XML root tag into UTF-8 encoding content.
func convert(xml []byte) (res []byte, err error) {
var (
patten = `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
matchStr, _ = gregex.MatchString(patten, string(xml))
xmlEncode = "UTF-8"
)
if len(matchStr) == 2 {
xmlEncode = matchStr[1]
}
xmlEncode = strings.ToUpper(xmlEncode)
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
}