-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml-reader.go
More file actions
80 lines (71 loc) · 1.88 KB
/
xml-reader.go
File metadata and controls
80 lines (71 loc) · 1.88 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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"time"
)
type Statement struct{
XMLName xml.Name `xml:"Statement"`
Data *struct{
Operations []struct{
PayDoc *struct{
Id string `xml:"id,attr"`
Details *struct {
Date docTime `xml:"DocDate"`
Sum float32 `xml:"Sum"`
Payer *struct{
Name string `xml:"Name"`
Inn string `xml:"INN"`
} `xml:"Payer"`
} `xml:"PayDocRu"`
} `xml:"PayDoc"`
DC int `xml:"DC"`
} `xml:"OperationInfo"`
} `xml:"Data"`
}
type docTime struct{
time.Time
}
func (t *docTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
err := d.DecodeElement(&v, &start)
if err != nil {
return err
}
parse, err := time.Parse("2006-01-02", v)
if err != nil {
return err
}
*t = docTime{parse}
return nil
}
func main() {
xmlFile, err := os.Open("./resources/payments.xml")
if err != nil {
fmt.Println(err)
}
defer xmlFile.Close()
byteValue, err := ioutil.ReadAll(xmlFile)
if err != nil {
fmt.Println(err)
}
var statement Statement
err = xml.Unmarshal(byteValue, &statement)
if err != nil {
fmt.Println(err)
}
for _, operation := range statement.Data.Operations {
if operation.PayDoc.Details != nil {
fmt.Println(fmt.Sprintf("id: %s; date: %s; sum: %f; name: %s; inn: %s; dc: %d",
operation.PayDoc.Id,
operation.PayDoc.Details.Date,
operation.PayDoc.Details.Sum,
operation.PayDoc.Details.Payer.Name,
operation.PayDoc.Details.Payer.Inn,
operation.DC,
))
}
}
}