Skip to content

Commit bd1562f

Browse files
committed
Support xml directive
1 parent c81d6f2 commit bd1562f

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

document.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
11
package xmldom
22

3+
import "bytes"
4+
35
type Document struct {
4-
ProcInst string
5-
Root *Node
6+
ProcInst string
7+
Directives []string
8+
Root *Node
69
}
710

811
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()
1019
}
1120

1221
func (d *Document) XMLPretty() string {
22+
buf := new(bytes.Buffer)
1323
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')
1530
}
16-
return d.Root.XMLPretty()
31+
buf.WriteString(d.Root.XMLPretty())
32+
return buf.String()
1733
}

dom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func Parse(r io.Reader) (*Document, error) {
7070
}
7171
case xml.ProcInst:
7272
doc.ProcInst = stringifyProcInst(&token)
73+
case xml.Directive:
74+
doc.Directives = append(doc.Directives, stringifyDirective(&token))
7375
}
7476

7577
// get the next token

example_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
const (
1010
ExampleXml = `<?xml version="1.0" encoding="UTF-8"?>
11+
<!DOCTYPE junit SYSTEM "junit-result.dtd">
1112
<testsuites>
1213
<testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
1314
<properties>
@@ -85,14 +86,15 @@ func ExampleDocument_XML() {
8586
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
8687
fmt.Println(doc.XML())
8788
// 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>
8990
}
9091

9192
func ExampleDocument_XMLPretty() {
9293
doc := xmldom.Must(xmldom.ParseXML(ExampleXml))
9394
fmt.Println(doc.XMLPretty())
9495
// Output:
9596
// <?xml version="1.0" encoding="UTF-8"?>
97+
// <!DOCTYPE junit SYSTEM "junit-result.dtd">
9698
// <testsuites>
9799
// <testsuite tests="2" failures="0" time="0.009" name="github.com/subchen/go-xmldom">
98100
// <properties>

print.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ func stringifyProcInst(pi *xml.ProcInst) string {
1414
return fmt.Sprintf("<?%s %s?>", pi.Target, string(pi.Inst))
1515
}
1616

17+
func stringifyDirective(directive *xml.Directive) string {
18+
if directive == nil {
19+
return ""
20+
}
21+
return fmt.Sprintf("<!%s>", string(*directive))
22+
}
23+
1724
func printXML(buf *bytes.Buffer, n *Node, level int, indent string) {
1825
pretty := len(indent) > 0
1926

0 commit comments

Comments
 (0)