xmlwriter

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2022 License: MIT Imports: 8 Imported by: 2

README

xmlwriter

A simpler and more powerful alternative to encoding/xml.Encoder for generating indented namespace-aware XML.

See the documentation and tests for examples and more information.

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundNS

type BoundNS struct {
	URI    NS
	Prefix Prefix
}

BoundNS is a bound namespace. The zero value is the default empty namespace.

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.

func (NS) Bind added in v0.0.4

func (n NS) Bind(p Prefix) BoundNS

Bind is a convenience function to create a BoundNS.

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.

func (Prefix) Bind added in v0.0.4

func (p Prefix) Bind(n NS) BoundNS

Bind is a convenience function to create a BoundNS.

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

func New(w io.Writer) *XMLWriter

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) BlankLine

func (x *XMLWriter) BlankLine() error

BlankLine adds a blank line if indentation is enabled.

func (*XMLWriter) Close

func (x *XMLWriter) Close() (err error)

Close checks for an error or unclosed elements. The writer cannot be used afterwards.

func (*XMLWriter) Comment

func (x *XMLWriter) Comment(indent bool, comment string) error

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

func (x *XMLWriter) DefaultProcInst() error

DefaultProcInst adds the default XML processing instruction.

func (*XMLWriter) Directive

func (x *XMLWriter) Directive(dir []byte) error

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

func (x *XMLWriter) End(self bool) error

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

func (x *XMLWriter) EndAuto() error

EndAuto is like End, but automatically chooses whether to use a self-closing end tag.

func (*XMLWriter) Err

func (x *XMLWriter) Err() error

Err checks for an error.

func (*XMLWriter) Indent

func (w *XMLWriter) Indent(s string)

Indent sets the string used for indentation. If s is empty, the output is not indented.

func (*XMLWriter) ProcInst

func (x *XMLWriter) ProcInst(target, inst string) error

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) Raw

func (x *XMLWriter) Raw(xml []byte) error

Raw adds the provided XML as-is.

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.

func (*XMLWriter) Text

func (x *XMLWriter) Text(indent bool, text string) error

Text adds a text node to the current element. 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 text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL