forked from heyvito/exml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.go
60 lines (52 loc) · 976 Bytes
/
print.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
package exml
import (
"fmt"
)
const (
quote = '"'
equal = '='
slash = '/'
tagStart = '<'
tagEnd = '>'
colon = ':'
xmlns = "xmlns"
space = ' '
linebreak = "\n"
)
func printXML(d Document) string {
b := newBuilder()
if d.ProcessingInstruction != "" {
b.writeString(fmt.Sprintf("<?%s?>\n", d.ProcessingInstruction))
}
r := d.root
if r == nil {
return b.String()
}
b.beginTag(0, r)
b.writeAttrs(r)
if len(d.Namespaces) > 0 {
for _, ns := range d.Namespaces {
b.writeAttr(xmlns, ns.Name, ns.URL)
}
}
if len(r.Children) == 0 && r.InnerText == "" {
b.writeRune(space)
b.writeRune(slash)
b.writeRune(tagEnd)
b.linebreak()
return b.String()
} else if r.InnerText != "" {
b.writeRune(tagEnd)
b.linebreak()
b.writeInnerText(1, r.InnerText)
b.linebreak()
} else {
b.writeRune(tagEnd)
b.linebreak()
for _, n := range r.Children {
b.writeNode(1, n)
}
}
b.endTag(0, r)
return b.String()
}