forked from subchen/go-xmldom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.go
76 lines (69 loc) · 1.48 KB
/
document.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
package xmldom
import (
"bytes"
"encoding/xml"
)
const (
DEFAULT_XML_HEADER = `<?xml version="1.0" encoding="UTF-8"?>`
xmlURL = "http://www.w3.org/XML/1998/namespace"
xmlnsPrefix = "xmlns"
xmlPrefix = "xml"
)
func NewDocument(name string) *Document {
d := &Document{
ProcInst: DEFAULT_XML_HEADER,
EmptyElementTag: true,
TextSafeMode: true,
}
d.Root = &Node{
Document: d,
Name: xml.Name{
Local: name,
},
}
return d
}
type Document struct {
ProcInst string
Directives []string
EmptyElementTag bool
TextSafeMode bool
Root *Node
}
func (d *Document) XML() string {
buf := new(bytes.Buffer)
buf.WriteString(d.ProcInst)
for _, directive := range d.Directives {
buf.WriteString(directive)
}
buf.WriteString(d.Root.XML())
return buf.String()
}
func (d *Document) XMLPretty() string {
buf := new(bytes.Buffer)
if len(d.ProcInst) > 0 {
buf.WriteString(d.ProcInst)
buf.WriteByte('\n')
}
for _, directive := range d.Directives {
buf.WriteString(directive)
buf.WriteByte('\n')
}
buf.WriteString(d.Root.XMLPretty())
buf.WriteByte('\n')
return buf.String()
}
func (d *Document) XMLPrettyEx(indent string) string {
buf := new(bytes.Buffer)
if len(d.ProcInst) > 0 {
buf.WriteString(d.ProcInst)
buf.WriteByte('\n')
}
for _, directive := range d.Directives {
buf.WriteString(directive)
buf.WriteByte('\n')
}
buf.WriteString(d.Root.XMLPrettyEx(indent))
buf.WriteByte('\n')
return buf.String()
}