jvxml

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: GPL-3.0, BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package jvxml implements an alternative encoder for XML data.

There are two differences between this package and the corresponding code in the standard library's encoding/xml package. First, this package implements an additional EmptyElement type to represent empty XML elements. Second, this package exports the IsName function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsName

func IsName(s []byte) bool

IsName reports whether the byte slice is a valid XML name.

Types

type EmptyElement

type EmptyElement struct {
	Name xml.Name
	Attr []xml.Attr
}

An EmptyElement represents an empty XML element.

func (EmptyElement) Copy

func (e EmptyElement) Copy() EmptyElement

Copy creates a new copy of StartElement.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder writes XML data to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Close

func (enc *Encoder) Close() error

Close the Encoder, indicating that no more data will be written. It flushes any buffered XML to the underlying writer and returns an error if the written XML is invalid (e.g. by containing unclosed elements).

func (*Encoder) EncodeToken

func (enc *Encoder) EncodeToken(t Token) error

EncodeToken writes the given XML token to the stream. It returns an error if [StartElement] and [EndElement] tokens are not properly matched.

EncodeToken does not call Encoder.Flush, because usually it is part of a larger operation such as [Encoder.Encode] or [Encoder.EncodeElement] (or a custom [Marshaler]'s MarshalXML invoked during those), and those will call Flush when finished. Callers that create an Encoder and then invoke EncodeToken directly, without using Encode or EncodeElement, need to call Flush when finished to ensure that the XML is written to the underlying writer.

EncodeToken allows writing a [ProcInst] with Target set to "xml" only as the first token in the stream.

func (*Encoder) Flush

func (enc *Encoder) Flush() error

Flush flushes any buffered XML to the underlying writer. See the Encoder.EncodeToken documentation for details about when it is necessary.

func (*Encoder) Indent

func (enc *Encoder) Indent(prefix, indent string)

Indent sets the encoder to generate XML in which each element begins on a new indented line that starts with prefix and is followed by one or more copies of indent according to the nesting depth.

type Token

type Token any

A Token is an interface holding one of the token types: xml.StartElement, xml.EndElement, EmptyElement, xml.CharData, xml.Comment, xml.ProcInst, or xml.Directive.

func CopyToken

func CopyToken(t Token) Token

CopyToken returns a copy of a Token.

Jump to

Keyboard shortcuts

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