File tree Expand file tree Collapse file tree 4 files changed +33
-6
lines changed Expand file tree Collapse file tree 4 files changed +33
-6
lines changed Original file line number Diff line number Diff line change 1
1
package xmldom
2
2
3
+ import "bytes"
4
+
3
5
type Document struct {
4
- ProcInst string
5
- Root * Node
6
+ ProcInst string
7
+ Directives []string
8
+ Root * Node
6
9
}
7
10
8
11
func (d * Document ) XML () string {
9
- return d .ProcInst + d .Root .XML ()
12
+ buf := new (bytes.Buffer )
13
+ buf .WriteString (d .ProcInst )
14
+ for _ , directive := range d .Directives {
15
+ buf .WriteString (directive )
16
+ }
17
+ buf .WriteString (d .Root .XML ())
18
+ return buf .String ()
10
19
}
11
20
12
21
func (d * Document ) XMLPretty () string {
22
+ buf := new (bytes.Buffer )
13
23
if len (d .ProcInst ) > 0 {
14
- return d .ProcInst + "\n " + d .Root .XMLPretty ()
24
+ buf .WriteString (d .ProcInst )
25
+ buf .WriteByte ('\n' )
26
+ }
27
+ for _ , directive := range d .Directives {
28
+ buf .WriteString (directive )
29
+ buf .WriteByte ('\n' )
15
30
}
16
- return d .Root .XMLPretty ()
31
+ buf .WriteString (d .Root .XMLPretty ())
32
+ return buf .String ()
17
33
}
Original file line number Diff line number Diff line change @@ -70,6 +70,8 @@ func Parse(r io.Reader) (*Document, error) {
70
70
}
71
71
case xml.ProcInst :
72
72
doc .ProcInst = stringifyProcInst (& token )
73
+ case xml.Directive :
74
+ doc .Directives = append (doc .Directives , stringifyDirective (& token ))
73
75
}
74
76
75
77
// get the next token
Original file line number Diff line number Diff line change 8
8
9
9
const (
10
10
ExampleXml = `<?xml version="1.0" encoding="UTF-8"?>
11
+ <!DOCTYPE junit SYSTEM "junit-result.dtd">
11
12
<testsuites>
12
13
<testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
13
14
<properties>
@@ -85,14 +86,15 @@ func ExampleDocument_XML() {
85
86
doc := xmldom .Must (xmldom .ParseXML (ExampleXml ))
86
87
fmt .Println (doc .XML ())
87
88
// Output:
88
- // <?xml version="1.0" encoding="UTF-8"?><testsuites><testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="go-xmldom" name="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" name="ExampleParse" time="0.005" /></testsuite></testsuites>
89
+ // <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE junit SYSTEM "junit-result.dtd">< testsuites><testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom"><properties><property name="go.version">go1.8.1</property></properties><testcase classname="go-xmldom" name="ExampleParseXML" time="0.004" /><testcase classname="go-xmldom" name="ExampleParse" time="0.005" /></testsuite></testsuites>
89
90
}
90
91
91
92
func ExampleDocument_XMLPretty () {
92
93
doc := xmldom .Must (xmldom .ParseXML (ExampleXml ))
93
94
fmt .Println (doc .XMLPretty ())
94
95
// Output:
95
96
// <?xml version="1.0" encoding="UTF-8"?>
97
+ // <!DOCTYPE junit SYSTEM "junit-result.dtd">
96
98
// <testsuites>
97
99
// <testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
98
100
// <properties>
Original file line number Diff line number Diff line change @@ -14,6 +14,13 @@ func stringifyProcInst(pi *xml.ProcInst) string {
14
14
return fmt .Sprintf ("<?%s %s?>" , pi .Target , string (pi .Inst ))
15
15
}
16
16
17
+ func stringifyDirective (directive * xml.Directive ) string {
18
+ if directive == nil {
19
+ return ""
20
+ }
21
+ return fmt .Sprintf ("<!%s>" , string (* directive ))
22
+ }
23
+
17
24
func printXML (buf * bytes.Buffer , n * Node , level int , indent string ) {
18
25
pretty := len (indent ) > 0
19
26
You can’t perform that action at this time.
0 commit comments