forked from jbussdieker/golibxml
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxml_encoder.go
55 lines (47 loc) · 1.38 KB
/
xml_encoder.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
package xml
/*
#include "xml.h"
*/
import "C"
import (
"errors"
"io"
"unsafe"
)
var ErrEncoding = errors.New("error encoding xml")
// EncodingOption is a set of flags used to control how a node is written out.
type EncodingOption int
const (
XML_SAVE_FORMAT EncodingOption = 1 << iota // format save output
XML_SAVE_NO_DECL //drop the xml declaration
XML_SAVE_NO_EMPTY //no empty tags
XML_SAVE_NO_XHTML //disable XHTML1 specific rules
XML_SAVE_XHTML //force XHTML1 specific rules
XML_SAVE_AS_XML //force XML serialization on HTML doc
XML_SAVE_AS_HTML //force HTML serialization on XML doc
XML_SAVE_WSNONSIG //format with non-significant whitespace
)
// Encoder encodes xml using a io.Writer as output
type Encoder struct {
node *Node
writer io.Writer
options EncodingOption
}
func NewEncoder(node *Node, writer io.Writer, opts EncodingOption) *Encoder {
return &Encoder{
node: node,
writer: writer,
options: opts,
}
}
func (enc *Encoder) Encode() error {
w := newWriter(enc.writer)
res := C.xmlEncode(w.UnsafePtr(), enc.node.Ptr, (*C.char)(unsafe.Pointer(nil)), C.int(enc.options))
if res < 0 {
if w.err != nil {
return w.err
}
return ErrEncoding
}
return nil
}