-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxmldata.go
71 lines (63 loc) · 1.25 KB
/
xmldata.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
package main
import (
"encoding/xml"
"io/ioutil"
"regexp"
)
type CardInfo struct {
ID string
Set string
SetID string
Name string
Number string
}
type set struct {
XMLName xml.Name `xml:"set"`
Name string `xml:"name,attr"`
ID string `xml:"id,attr"`
Cards []card `xml:"cards>card"`
}
type card struct {
ID string `xml:"id,attr"`
Name string `xml:"name,attr"`
Details []prop `xml:"property"`
}
type prop struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
}
var (
re = regexp.MustCompile("[0-9]+")
)
//Parse a set xml file and return slice of cards
func parseSetXML(xmlPath string) (results []CardInfo, err error) {
//read data from xml file
data, err := ioutil.ReadFile(xmlPath)
if err != nil {
return results, err
}
//parse that xml data
v := new(set)
err = xml.Unmarshal(data, &v)
if err != nil {
return results, err
}
//parse out all the cards
for _, cur := range v.Cards {
cNumber := ""
for _, det := range cur.Details {
if det.Name == "CardNumber" {
cNumber = re.FindString(det.Value)
}
}
newItem := CardInfo{
ID: cur.ID,
Name: cur.Name,
Set: v.Name,
SetID: v.ID,
Number: cNumber,
}
results = append(results, newItem)
}
return
}