Documentation ¶
Overview ¶
Package xmlwriter provides a lightweight XML encoder with basic namespace awareness and formatting.
Unlike encoding/xml.Encoder, it supports self-closing tags, has a simpler interface, and provides more control over indentation and namespaces.
Index ¶
- type BoundNS
- type NS
- type NamespaceMatcher
- type Prefix
- type XMLWriter
- func (x *XMLWriter) Attr(ns NamespaceMatcher, name, value string) error
- func (x *XMLWriter) BlankLine() error
- func (x *XMLWriter) Close() (err error)
- func (x *XMLWriter) Comment(indent bool, comment string) error
- func (x *XMLWriter) DefaultProcInst() error
- func (x *XMLWriter) Directive(dir []byte) error
- func (x *XMLWriter) End(self bool) error
- func (x *XMLWriter) EndAuto() error
- func (x *XMLWriter) Err() error
- func (w *XMLWriter) Indent(s string)
- func (x *XMLWriter) ProcInst(target, inst string) error
- func (x *XMLWriter) Raw(xml []byte) error
- func (x *XMLWriter) Start(ns NamespaceMatcher, name string, define ...BoundNS) error
- func (x *XMLWriter) Text(indent bool, text string) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NS ¶
type NS string
NS is a namespace URI. An empty URI is the empty namespace.
const NamespaceXML NS = "http://www.w3.org/XML/1998/namespace"
NamespaceXML is the built-in namespace bound to the xml prefix.
type NamespaceMatcher ¶
type NamespaceMatcher interface {
// contains filtered or unexported methods
}
NamespaceMatcher represents the desired namespace for an element or attribute.
type Prefix ¶
type Prefix string
Prefix is a namespace prefix. An empty prefix is the default namespace.
type XMLWriter ¶
type XMLWriter struct {
// contains filtered or unexported fields
}
XMLWriter generates XML. All errors are sticky and can be retrieved with (*XMLWriter).Err() or (*XMLWriter).Close().
func New ¶
New creates a new *XMLWriter writing to w.
Example (Xhtml) ¶
const ( xhtml NS = "http://www.w3.org/1999/xhtml" svg NS = "http://www.w3.org/2000/svg" xlink NS = "http://www.w3.org/1999/xlink" ) x := New(os.Stdout) x.Indent(" ") x.DefaultProcInst() x.Directive([]byte("DOCTYPE html")) x.Start(xhtml, "html", xhtml.Bind("")) { x.Start(xhtml, "head") { x.Start(xhtml, "title") x.Text(false, "Document Title") x.End(false) } { x.Start(xhtml, "link") x.Attr(xhtml, "rel", "stylesheet") x.Attr(xhtml, "href", "style.css") x.End(true) } { x.Start(xhtml, "style") x.Text(true, "svg {") x.Text(true, " width: 20px;") x.Text(true, " fill: black;") x.Text(true, "}") x.End(false) } x.End(false) } { x.Start(xhtml, "body") { x.Start(xhtml, "p") x.Text(false, "This is an example document generated by ") { x.Start(xhtml, "i") x.Text(false, "xmlwriter") x.End(false) } x.Text(false, ".") x.End(false) } x.BlankLine() { x.Start(svg, "svg", svg.Bind(""), xlink.Bind("xlink")) x.Attr(svg, "viewBox", "0 0 20 20") { x.Start(svg, "a") x.Attr(xlink, "href", "https://example.com") { x.Start(svg, "circle") x.Attr(svg, "cx", "10") x.Attr(svg, "cy", "10") x.Attr(svg, "r", "8") x.End(true) } x.End(false) } x.End(false) } x.End(false) } x.End(false) if err := x.Close(); err != nil { fmt.Fprintf(os.Stderr, "\nError: %v\n", err) os.Exit(1) }
Output: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Document Title</title> <link rel="stylesheet" href="style.css"/> <style> svg { width: 20px; fill: black; } </style> </head> <body> <p>This is an example document generated by <i>xmlwriter</i>.</p> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 20 20"> <a xlink:href="https://example.com"> <circle cx="10" cy="10" r="8"/> </a> </svg> </body> </html>
func (*XMLWriter) Attr ¶
func (x *XMLWriter) Attr(ns NamespaceMatcher, name, value string) error
Attr adds an attribute in ns (the current one if nil) to the current element. If the element has children or the attribute has already been defined, an error is returned.
func (*XMLWriter) Close ¶
Close checks for an error or unclosed elements. The writer cannot be used afterwards.
func (*XMLWriter) Comment ¶
Comment adds a comment. If indentation is enabled, indent is true, and the current element is empty or its last child was not an unidented text node or comment, then a newline and the indentation is written before the comment. If it does not have a parent and indentation is enabled, a newline is written after the closing tag.
func (*XMLWriter) DefaultProcInst ¶
DefaultProcInst adds the default XML processing instruction.
func (*XMLWriter) Directive ¶
Directive adds a directive. If indentation is enabled, and the current element is empty or its last child was not an unidented text node or comment then a newline and the indentation is written before the instruction. If it does not have a parent and indentation is enabled, a newline is written after the closing tag.
func (*XMLWriter) End ¶
End ends the current tag. If self is true, a self-closing tag is written and an error is returned if the element has children. If indentation is enabled, self is false, the current element contains children and its last child was not an unidented text node or comment, then a newline and the indentation is written before the closing tag. If the current element is the last one and indentation is enabled, a newline is written after the closing tag.
func (*XMLWriter) EndAuto ¶ added in v0.0.4
EndAuto is like End, but automatically chooses whether to use a self-closing end tag.
func (*XMLWriter) Indent ¶
Indent sets the string used for indentation. If s is empty, the output is not indented.
func (*XMLWriter) ProcInst ¶
ProcInst adds a processing instruction. If indentation is enabled, and the current element is empty or its last child was not an unidented text node or comment, then a newline and the indentation is written before the instruction. If it does not have a parent and indentation is enabled, a newline is written after the closing tag.
func (*XMLWriter) Start ¶
func (x *XMLWriter) Start(ns NamespaceMatcher, name string, define ...BoundNS) error
Start starts a new element in ns (the current one if nil), defining the provided namespaces, if any. If indentation is enabled, and the current element is empty or its last child was not an unidented text node or comment, then a newline and the indentation is written before the opening tag.