-
Notifications
You must be signed in to change notification settings - Fork 11
/
tablib_xml.go
64 lines (54 loc) · 1.8 KB
/
tablib_xml.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
package tablib
import (
"bytes"
"github.com/agrison/mxj"
)
// XML returns a XML representation of the Dataset as an Exportable.
func (d *Dataset) XML() (*Exportable, error) {
return d.XMLWithTagNamePrefixIndent("row", " ", " ")
}
// XML returns a XML representation of the Databook as an Exportable.
func (d *Databook) XML() (*Exportable, error) {
b := newBuffer()
b.WriteString("<databook>\n")
for _, s := range d.sheets {
b.WriteString(" <sheet>\n <title>" + s.title + "</title>\n ")
row, err := s.dataset.XMLWithTagNamePrefixIndent("row", " ", " ")
if err != nil {
return nil, err
}
b.Write(row.Bytes())
b.WriteString("\n </sheet>")
}
b.WriteString("\n</databook>")
return newExportable(b), nil
}
// XMLWithTagNamePrefixIndent returns a XML representation with custom tag, prefix and indent.
func (d *Dataset) XMLWithTagNamePrefixIndent(tagName, prefix, indent string) (*Exportable, error) {
back := d.Dict()
exportable := newExportable(newBuffer())
exportable.buffer.WriteString("<dataset>\n")
for _, r := range back {
m := mxj.Map(r.(map[string]interface{}))
if err := m.XmlIndentWriter(exportable.buffer, prefix, indent, tagName); err != nil {
return nil, err
}
}
exportable.buffer.WriteString("\n" + prefix + "</dataset>")
return exportable, nil
}
// LoadXML loads a Dataset from an XML source.
func LoadXML(input []byte) (*Dataset, error) {
m, _, err := mxj.NewMapXmlReaderRaw(bytes.NewReader(input))
if err != nil {
return nil, err
}
// this seems quite a bit hacky
datasetNode, _ := m.ValueForPath("dataset")
rowNode := datasetNode.(map[string]interface{})["row"].([]interface{})
back := make([]map[string]interface{}, 0, len(rowNode))
for _, r := range rowNode {
back = append(back, r.(map[string]interface{}))
}
return internalLoadFromDict(back)
}