Package xml

import "encoding/xml"
Overview
Index
Examples

Overview ▾

Package xml implements a simple XML 1.0 parser that understands XML name spaces.

Example (CustomMarshalXML)

Zoo Census:
* Gophers: 3
* Zebras:  2
* Unknown: 3

Example (TextMarshalXML)

Inventory Counts:
* Small:        3
* Large:        2
* Unrecognized: 3

Index ▾

Constants
Variables
func Escape(w io.Writer, s []byte)
func EscapeText(w io.Writer, s []byte) error
func Marshal(v any) ([]byte, error)
func MarshalIndent(v any, prefix, indent string) ([]byte, error)
func Unmarshal(data []byte, v any) error
type Attr
type CharData
    func (c CharData) Copy() CharData
type Comment
    func (c Comment) Copy() Comment
type Decoder
    func NewDecoder(r io.Reader) *Decoder
    func NewTokenDecoder(t TokenReader) *Decoder
    func (d *Decoder) Decode(v any) error
    func (d *Decoder) DecodeElement(v any, start *StartElement) error
    func (d *Decoder) InputOffset() int64
    func (d *Decoder) InputPos() (line, column int)
    func (d *Decoder) RawToken() (Token, error)
    func (d *Decoder) Skip() error
    func (d *Decoder) Token() (Token, error)
type Directive
    func (d Directive) Copy() Directive
type Encoder
    func NewEncoder(w io.Writer) *Encoder
    func (enc *Encoder) Close() error
    func (enc *Encoder) Encode(v any) error
    func (enc *Encoder) EncodeElement(v any, start StartElement) error
    func (enc *Encoder) EncodeToken(t Token) error
    func (enc *Encoder) Flush() error
    func (enc *Encoder) Indent(prefix, indent string)
type EndElement
type Marshaler
type MarshalerAttr
type Name
type ProcInst
    func (p ProcInst) Copy() ProcInst
type StartElement
    func (e StartElement) Copy() StartElement
    func (e StartElement) End() EndElement
type SyntaxError
    func (e *SyntaxError) Error() string
type TagPathError
    func (e *TagPathError) Error() string
type Token
    func CopyToken(t Token) Token
type TokenReader
type UnmarshalError
    func (e UnmarshalError) Error() string
type Unmarshaler
type UnmarshalerAttr
type UnsupportedTypeError
    func (e *UnsupportedTypeError) Error() string
Bugs

Package files

marshal.go read.go typeinfo.go xml.go

Constants

const (
    // Header is a generic XML header suitable for use with the output of [Marshal].
    // This is not automatically added to any output of this package,
    // it is provided as a convenience.
    Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
)

Variables

HTMLAutoClose is the set of HTML elements that should be considered to close automatically.

See the [Decoder.Strict] and [Decoder.Entity] fields' documentation.

var HTMLAutoClose []string = htmlAutoClose

HTMLEntity is an entity map containing translations for the standard HTML entity characters.

See the [Decoder.Strict] and [Decoder.Entity] fields' documentation.

var HTMLEntity map[string]string = htmlEntity

func Escape

func Escape(w io.Writer, s []byte)

Escape is like EscapeText but omits the error return value. It is provided for backwards compatibility with Go 1.0. Code targeting Go 1.1 or later should use EscapeText.

func EscapeText 1.1

func EscapeText(w io.Writer, s []byte) error

EscapeText writes to w the properly escaped XML equivalent of the plain text data s.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal returns the XML encoding of v.

Marshal handles an array or slice by marshaling each of the elements. Marshal handles a pointer by marshaling the value it points at or, if the pointer is nil, by writing nothing. Marshal handles an interface value by marshaling the value it contains or, if the interface value is nil, by writing nothing. Marshal handles all other data by writing one or more XML elements containing the data.

The name for the XML elements is taken from, in order of preference:

The XML element for a struct contains marshaled elements for each of the exported fields of the struct, with these exceptions:

If a field uses a tag "a>b>c", then the element c will be nested inside parent elements a and b. Fields that appear next to each other that name the same parent will be enclosed in one XML element.

If the XML name for a struct field is defined by both the field tag and the struct's XMLName field, the names must match.

See MarshalIndent for an example.

Marshal will return an error if asked to marshal a channel, function, or map.

func MarshalIndent

func MarshalIndent(v any, prefix, indent string) ([]byte, error)

MarshalIndent works like Marshal, but each XML 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.

Example

  <person id="13">
      <name>
          <first>John</first>
          <last>Doe</last>
      </name>
      <age>42</age>
      <Married>false</Married>
      <City>Hanga Roa</City>
      <State>Easter Island</State>
      <!-- Need more details. -->
  </person>

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal parses the XML-encoded data and stores the result in the value pointed to by v, which must be an arbitrary struct, slice, or string. Well-formed data that does not fit into v is discarded.

Because Unmarshal uses the reflect package, it can only assign to exported (upper case) fields. Unmarshal uses a case-sensitive comparison to match XML element names to tag values and struct field names.

Unmarshal maps an XML element to a struct using the following rules. In the rules, the tag of a field refers to the value associated with the key 'xml' in the struct field's tag (see the example above).

If Unmarshal encounters a field type that implements the Unmarshaler interface, Unmarshal calls its UnmarshalXML method to produce the value from the XML element. Otherwise, if the value implements encoding.TextUnmarshaler, Unmarshal calls that value's UnmarshalText method.

Unmarshal maps an XML element to a string or []byte by saving the concatenation of that element's character data in the string or []byte. The saved []byte is never nil.

Unmarshal maps an attribute value to a string or []byte by saving the value in the string or slice.

Unmarshal maps an attribute value to an Attr by saving the attribute, including its name, in the Attr.

Unmarshal maps an XML element or attribute value to a slice by extending the length of the slice and mapping the element or attribute to the newly created value.

Unmarshal maps an XML element or attribute value to a bool by setting it to the boolean value represented by the string. Whitespace is trimmed and ignored.

Unmarshal maps an XML element or attribute value to an integer or floating-point field by setting the field to the result of interpreting the string value in decimal. There is no check for overflow. Whitespace is trimmed and ignored.

Unmarshal maps an XML element to a Name by recording the element name.

Unmarshal maps an XML element to a pointer by setting the pointer to a freshly allocated value and then mapping the element to that value.

A missing element or empty attribute value will be unmarshaled as a zero value. If the field is a slice, a zero value will be appended to the field. Otherwise, the field will be set to its zero value.

Example

This example demonstrates unmarshaling an XML excerpt into a value with some preset fields. Note that the Phone field isn't modified and that the XML <Company> element is ignored. Also, the Groups field is assigned considering the element path provided in its tag.

XMLName: xml.Name{Space:"", Local:"Person"}
Name: "Grace R. Emlin"
Phone: "none"
Email: [{home gre@example.com} {work gre@work.com}]
Groups: [Friends Squash]
Address: {Hanga Roa Easter Island}

type Attr

An Attr represents an attribute in an XML element (Name=Value).

type Attr struct {
    Name  Name
    Value string
}

type CharData

A CharData represents XML character data (raw text), in which XML escape sequences have been replaced by the characters they represent.

type CharData []byte

func (CharData) Copy

func (c CharData) Copy() CharData

Copy creates a new copy of CharData.

type Comment

A Comment represents an XML comment of the form <!--comment-->. The bytes do not include the <!-- and --> comment markers.

type Comment []byte

func (Comment) Copy

func (c Comment) Copy() Comment

Copy creates a new copy of Comment.

type Decoder

A Decoder represents an XML parser reading a particular input stream. The parser assumes that its input is encoded in UTF-8.

type Decoder struct {
    // Strict defaults to true, enforcing the requirements
    // of the XML specification.
    // If set to false, the parser allows input containing common
    // mistakes:
    //	* If an element is missing an end tag, the parser invents
    //	  end tags as necessary to keep the return values from Token
    //	  properly balanced.
    //	* In attribute values and character data, unknown or malformed
    //	  character entities (sequences beginning with &) are left alone.
    //
    // Setting:
    //
    //	d.Strict = false
    //	d.AutoClose = xml.HTMLAutoClose
    //	d.Entity = xml.HTMLEntity
    //
    // creates a parser that can handle typical HTML.
    //
    // Strict mode does not enforce the requirements of the XML name spaces TR.
    // In particular it does not reject name space tags using undefined prefixes.
    // Such tags are recorded with the unknown prefix as the name space URL.
    Strict bool

    // When Strict == false, AutoClose indicates a set of elements to
    // consider closed immediately after they are opened, regardless
    // of whether an end element is present.
    AutoClose []string

    // Entity can be used to map non-standard entity names to string replacements.
    // The parser behaves as if these standard mappings are present in the map,
    // regardless of the actual map content:
    //
    //	"lt": "<",
    //	"gt": ">",
    //	"amp": "&",
    //	"apos": "'",
    //	"quot": `"`,
    Entity map[string]string

    // CharsetReader, if non-nil, defines a function to generate
    // charset-conversion readers, converting from the provided
    // non-UTF-8 charset into UTF-8. If CharsetReader is nil or
    // returns an error, parsing stops with an error. One of the
    // CharsetReader's result values must be non-nil.
    CharsetReader func(charset string, input io.Reader) (io.Reader, error)

    // DefaultSpace sets the default name space used for unadorned tags,
    // as if the entire XML stream were wrapped in an element containing
    // the attribute xmlns="DefaultSpace".
    DefaultSpace string // Go 1.1
    // contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder creates a new XML parser reading from r. If r does not implement io.ByteReader, NewDecoder will do its own buffering.

func NewTokenDecoder 1.10

func NewTokenDecoder(t TokenReader) *Decoder

NewTokenDecoder creates a new XML parser using an underlying token stream.

func (*Decoder) Decode

func (d *Decoder) Decode(v any) error

Decode works like Unmarshal, except it reads the decoder stream to find the start element.

func (*Decoder) DecodeElement

func (d *Decoder) DecodeElement(v any, start *StartElement) error

DecodeElement works like Unmarshal except that it takes a pointer to the start XML element to decode into v. It is useful when a client reads some raw XML tokens itself but also wants to defer to Unmarshal for some elements.

func (*Decoder) InputOffset 1.4

func (d *Decoder) InputOffset() int64

InputOffset returns the input stream byte offset of the current decoder position. The offset gives the location of the end of the most recently returned token and the beginning of the next token.

func (*Decoder) InputPos 1.19

func (d *Decoder) InputPos() (line, column int)

InputPos returns the line of the current decoder position and the 1 based input position of the line. The position gives the location of the end of the most recently returned token.

func (*Decoder) RawToken

func (d *Decoder) RawToken() (Token, error)

RawToken is like Decoder.Token but does not verify that start and end elements match and does not translate name space prefixes to their corresponding URLs.

func (*Decoder) Skip

func (d *Decoder) Skip() error

Skip reads tokens until it has consumed the end element matching the most recent start element already consumed, skipping nested structures. It returns nil if it finds an end element matching the start element; otherwise it returns an error describing the problem.

func (*Decoder) Token

func (d *Decoder) Token() (Token, error)

Token returns the next XML token in the input stream. At the end of the input stream, Token returns nil, io.EOF.

Slices of bytes in the returned token data refer to the parser's internal buffer and remain valid only until the next call to Token. To acquire a copy of the bytes, call CopyToken or the token's Copy method.

Token expands self-closing elements such as <br> into separate start and end elements returned by successive calls.

Token guarantees that the StartElement and EndElement tokens it returns are properly nested and matched: if Token encounters an unexpected end element or EOF before all expected end elements, it will return an error.

If [Decoder.CharsetReader] is called and returns an error, the error is wrapped and returned.

Token implements XML name spaces as described by https://www.w3.org/TR/REC-xml-names/. Each of the Name structures contained in the Token has the Space set to the URL identifying its name space when known. If Token encounters an unrecognized name space prefix, it uses the prefix as the Space rather than report an error.

type Directive

A Directive represents an XML directive of the form <!text>. The bytes do not include the <! and > markers.

type Directive []byte

func (Directive) Copy

func (d Directive) Copy() Directive

Copy creates a new copy of Directive.

type Encoder

An Encoder writes XML data to an output stream.

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

Example

  <person id="13">
      <name>
          <first>John</first>
          <last>Doe</last>
      </name>
      <age>42</age>
      <Married>false</Married>
      <City>Hanga Roa</City>
      <State>Easter Island</State>
      <!-- Need more details. -->
  </person>

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Close 1.20

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

func (enc *Encoder) Encode(v any) error

Encode writes the XML encoding of v to the stream.

See the documentation for Marshal for details about the conversion of Go values to XML.

Encode calls Encoder.Flush before returning.

func (*Encoder) EncodeElement 1.2

func (enc *Encoder) EncodeElement(v any, start StartElement) error

EncodeElement writes the XML encoding of v to the stream, using start as the outermost tag in the encoding.

See the documentation for Marshal for details about the conversion of Go values to XML.

EncodeElement calls Encoder.Flush before returning.

func (*Encoder) EncodeToken 1.2

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 1.2

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 1.1

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 EndElement

An EndElement represents an XML end element.

type EndElement struct {
    Name Name
}

type Marshaler 1.2

Marshaler is the interface implemented by objects that can marshal themselves into valid XML elements.

MarshalXML encodes the receiver as zero or more XML elements. By convention, arrays or slices are typically encoded as a sequence of elements, one per entry. Using start as the element tag is not required, but doing so will enable Unmarshal to match the XML elements to the correct struct field. One common implementation strategy is to construct a separate value with a layout corresponding to the desired XML and then to encode it using e.EncodeElement. Another common strategy is to use repeated calls to e.EncodeToken to generate the XML output one token at a time. The sequence of encoded tokens must make up zero or more valid XML elements.

type Marshaler interface {
    MarshalXML(e *Encoder, start StartElement) error
}

type MarshalerAttr 1.2

MarshalerAttr is the interface implemented by objects that can marshal themselves into valid XML attributes.

MarshalXMLAttr returns an XML attribute with the encoded value of the receiver. Using name as the attribute name is not required, but doing so will enable Unmarshal to match the attribute to the correct struct field. If MarshalXMLAttr returns the zero attribute Attr{}, no attribute will be generated in the output. MarshalXMLAttr is used only for struct fields with the "attr" option in the field tag.

type MarshalerAttr interface {
    MarshalXMLAttr(name Name) (Attr, error)
}

type Name

A Name represents an XML name (Local) annotated with a name space identifier (Space). In tokens returned by Decoder.Token, the Space identifier is given as a canonical URL, not the short prefix used in the document being parsed.

type Name struct {
    Space, Local string
}

type ProcInst

A ProcInst represents an XML processing instruction of the form <?target inst?>

type ProcInst struct {
    Target string
    Inst   []byte
}

func (ProcInst) Copy

func (p ProcInst) Copy() ProcInst

Copy creates a new copy of ProcInst.

type StartElement

A StartElement represents an XML start element.

type StartElement struct {
    Name Name
    Attr []Attr
}

func (StartElement) Copy

func (e StartElement) Copy() StartElement

Copy creates a new copy of StartElement.

func (StartElement) End 1.2

func (e StartElement) End() EndElement

End returns the corresponding XML end element.

type SyntaxError

A SyntaxError represents a syntax error in the XML input stream.

type SyntaxError struct {
    Msg  string
    Line int
}

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type TagPathError

A TagPathError represents an error in the unmarshaling process caused by the use of field tags with conflicting paths.

type TagPathError struct {
    Struct       reflect.Type
    Field1, Tag1 string
    Field2, Tag2 string
}

func (*TagPathError) Error

func (e *TagPathError) Error() string

type Token

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

type Token any

func CopyToken

func CopyToken(t Token) Token

CopyToken returns a copy of a Token.

type TokenReader 1.10

A TokenReader is anything that can decode a stream of XML tokens, including a Decoder.

When Token encounters an error or end-of-file condition after successfully reading a token, it returns the token. It may return the (non-nil) error from the same call or return the error (and a nil token) from a subsequent call. An instance of this general case is that a TokenReader returning a non-nil token at the end of the token stream may return either io.EOF or a nil error. The next Read should return nil, io.EOF.

Implementations of Token are discouraged from returning a nil token with a nil error. Callers should treat a return of nil, nil as indicating that nothing happened; in particular it does not indicate EOF.

type TokenReader interface {
    Token() (Token, error)
}

type UnmarshalError

An UnmarshalError represents an error in the unmarshaling process.

type UnmarshalError string

func (UnmarshalError) Error

func (e UnmarshalError) Error() string

type Unmarshaler 1.2

Unmarshaler is the interface implemented by objects that can unmarshal an XML element description of themselves.

UnmarshalXML decodes a single XML element beginning with the given start element. If it returns an error, the outer call to Unmarshal stops and returns that error. UnmarshalXML must consume exactly one XML element. One common implementation strategy is to unmarshal into a separate value with a layout matching the expected XML using d.DecodeElement, and then to copy the data from that value into the receiver. Another common strategy is to use d.Token to process the XML object one token at a time. UnmarshalXML may not use d.RawToken.

type Unmarshaler interface {
    UnmarshalXML(d *Decoder, start StartElement) error
}

type UnmarshalerAttr 1.2

UnmarshalerAttr is the interface implemented by objects that can unmarshal an XML attribute description of themselves.

UnmarshalXMLAttr decodes a single XML attribute. If it returns an error, the outer call to Unmarshal stops and returns that error. UnmarshalXMLAttr is used only for struct fields with the "attr" option in the field tag.

type UnmarshalerAttr interface {
    UnmarshalXMLAttr(attr Attr) error
}

type UnsupportedTypeError

UnsupportedTypeError is returned when Marshal encounters a type that cannot be converted into XML.

type UnsupportedTypeError struct {
    Type reflect.Type
}

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

Bugs