/
core.go
85 lines (74 loc) · 1.71 KB
/
core.go
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
package xmlDeserializer
import (
"encoding/xml"
"github.com/beevik/etree"
"strings"
)
func getChildrenByTagName(root *etree.Element, name string) []*etree.Element {
var resSlice []*etree.Element
selectNode := root
arr := strings.Split(name, ">")
for i := 0; i < len(arr); i++ {
subName := arr[i]
children := selectNode.ChildElements()
for _, node := range children {
if node.Tag == subName {
if i == len(arr)-1 {
resSlice = append(resSlice, node)
continue
}
selectNode = node
break
}
}
}
return resSlice
}
func getChildByTagName(root *etree.Element, name string) *etree.Element {
selectNode := root
arr := strings.Split(name, ">")
for i := 0; i < len(arr); i++ {
subName := arr[i]
children := selectNode.ChildElements()
for _, node := range children {
if node.Tag == subName {
if i == len(arr)-1 {
return node
}
selectNode = node
break
}
}
}
return nil
}
func unmarshalByElement(root *etree.Element, instance interface{}) error {
xmlStr := GetElementXml(root, true)
err := xml.Unmarshal([]byte(xmlStr), instance)
if err != nil {
return err
}
return nil
}
func checkIsPrefixXmlTag(codeXmlTag string, prefix string) bool {
facIdx := strings.LastIndex(codeXmlTag, prefix+".")
if facIdx < 0 {
return false
}
return true
}
func getMapTypeNameFromXmlTag(codeXmlTag string, prefix string) string {
facIdx := strings.LastIndex(codeXmlTag, prefix+".")
if facIdx < 0 {
return ""
}
mapTypeName := codeXmlTag[facIdx+len(prefix)+1:]
return mapTypeName
}
/*
return parent line string and tail string
*/
func parsePrefixXmlTag(codeXmlTag string) ([]string, string) {
arr := strings.Split(codeXmlTag, ">")
return arr[:len(arr)-1], arr[len(arr)-1]
}