xml

Imports

Imports #

"bufio"
"bytes"
"encoding"
"errors"
"fmt"
"io"
"reflect"
"strconv"
"strings"
"bytes"
"encoding"
"errors"
"fmt"
"reflect"
"runtime"
"strconv"
"strings"
"fmt"
"reflect"
"strings"
"sync"
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strconv"
"strings"
"unicode"
"unicode/utf8"

Constants & Variables

HTMLAutoClose var #

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 var #

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

attrType var #

var attrType = *ast.CallExpr

begComment var #

var begComment = *ast.CallExpr

cdataEnd var #

var cdataEnd = *ast.CallExpr

cdataEscape var #

var cdataEscape = *ast.CallExpr

cdataStart var #

var cdataStart = *ast.CallExpr

ddBytes var #

var ddBytes = *ast.CallExpr

dontInitNilPointers const #

const dontInitNilPointers = false

endComment var #

var endComment = *ast.CallExpr

endProcInst var #

var endProcInst = *ast.CallExpr

entity var #

var entity = map[string]rune{...}

errRawToken var #

var errRawToken = *ast.CallExpr

errUnmarshalDepth var #

var errUnmarshalDepth = *ast.CallExpr

escAmp var #

var escAmp = *ast.CallExpr

escApos var #

var escApos = *ast.CallExpr

escCR var #

var escCR = *ast.CallExpr

escFFFD var #

var escFFFD = *ast.CallExpr

escGT var #

var escGT = *ast.CallExpr

escLT var #

var escLT = *ast.CallExpr

escNL var #

var escNL = *ast.CallExpr

escQuot var #

var escQuot = *ast.CallExpr

escTab var #

var escTab = *ast.CallExpr

fAny const #

const fAny

fAttr const #

const fAttr

fCDATA const #

const fCDATA

fCharData const #

const fCharData

fComment const #

const fComment

fElement const #

const fElement fieldFlags = *ast.BinaryExpr

fInnerXML const #

const fInnerXML

fMode const #

const fMode = *ast.BinaryExpr

fOmitEmpty const #

const fOmitEmpty

first var #

var first = *ast.UnaryExpr

htmlAutoClose var #

var htmlAutoClose = []string{...}

htmlEntity var #

var htmlEntity = map[string]string{...}

initNilPointers const #

const initNilPointers = true

marshalerAttrType var #

var marshalerAttrType = *ast.CallExpr

marshalerType var #

var marshalerType = *ast.CallExpr

maxUnmarshalDepth const #

const maxUnmarshalDepth = 10000

maxUnmarshalDepthWasm const #

const maxUnmarshalDepthWasm = 5000

nameType var #

var nameType = *ast.CallExpr

second var #

var second = *ast.UnaryExpr

stkEOF const #

const stkEOF

stkNs const #

const stkNs

stkStart const #

const stkStart = iota

textMarshalerType var #

var textMarshalerType = *ast.CallExpr

textUnmarshalerType var #

var textUnmarshalerType = *ast.CallExpr

tinfoMap var #

var tinfoMap sync.Map

unmarshalerAttrType var #

var unmarshalerAttrType = *ast.CallExpr

unmarshalerType var #

var unmarshalerType = *ast.CallExpr

xmlName const #

const xmlName = "XMLName"

xmlPrefix const #

const xmlPrefix = "xml"

xmlURL const #

const xmlURL = "http://www.w3.org/XML/1998/namespace"

xmlnsPrefix const #

const xmlnsPrefix = "xmlns"

Type Aliases

CharData type #

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

type CharData []byte

Comment type #

A Comment represents an XML comment of the form . The bytes do not include the comment markers.

type Comment []byte

Directive type #

A Directive represents an XML directive of the form . The bytes do not include the markers.

type Directive []byte

Token type #

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

type Token any

UnmarshalError type #

An UnmarshalError represents an error in the unmarshaling process.

type UnmarshalError string

fieldFlags type #

type fieldFlags int

Interfaces

Marshaler interface #

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
}

MarshalerAttr interface #

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

TokenReader interface #

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

Unmarshaler interface #

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
}

UnmarshalerAttr interface #

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
}

Structs

Attr struct #

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

type Attr struct {
Name Name
Value string
}

Decoder struct #

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 bool
AutoClose []string
Entity map[string]string
CharsetReader func(charset string, input io.Reader) (io.Reader, error)
DefaultSpace string
r io.ByteReader
t TokenReader
buf bytes.Buffer
saved *bytes.Buffer
stk *stack
free *stack
needClose bool
toClose Name
nextToken Token
nextByte int
ns map[string]string
err error
line int
linestart int64
offset int64
unmarshalDepth int
}

Encoder struct #

An Encoder writes XML data to an output stream.

type Encoder struct {
p printer
}

EndElement struct #

An EndElement represents an XML end element.

type EndElement struct {
Name Name
}

Name struct #

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 string
Local string
}

ProcInst struct #

A ProcInst represents an XML processing instruction of the form

type ProcInst struct {
Target string
Inst []byte
}

StartElement struct #

A StartElement represents an XML start element.

type StartElement struct {
Name Name
Attr []Attr
}

SyntaxError struct #

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

type SyntaxError struct {
Msg string
Line int
}

TagPathError struct #

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 string
Tag1 string
Field2 string
Tag2 string
}

UnsupportedTypeError struct #

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

type UnsupportedTypeError struct {
Type reflect.Type
}

fieldInfo struct #

fieldInfo holds details for the xml representation of a single field.

type fieldInfo struct {
idx []int
name string
xmlns string
flags fieldFlags
parents []string
}

parentStack struct #

type parentStack struct {
p *printer
stack []string
}

printer struct #

type printer struct {
w *bufio.Writer
encoder *Encoder
seq int
indent string
prefix string
depth int
indentedIn bool
putNewline bool
attrNS map[string]string
attrPrefix map[string]string
prefixes []string
tags []Name
closed bool
err error
}

stack struct #

Parsing state - stack holds old name space translations and the current set of open elements. The translations to pop when ending a given tag are *below* it on the stack, which is more work but forced on us by XML.

type stack struct {
next *stack
kind int
name Name
ok bool
}

typeInfo struct #

typeInfo holds details for the xml representation of a type.

type typeInfo struct {
xmlname *fieldInfo
fields []fieldInfo
}

Functions

Close method #

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 (p *printer) Close() error

Close method #

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 (enc *Encoder) Close() error

Copy method #

Copy creates a new copy of ProcInst.

func (p ProcInst) Copy() ProcInst

Copy method #

Copy creates a new copy of CharData.

func (c CharData) Copy() CharData

Copy method #

Copy creates a new copy of Comment.

func (c Comment) Copy() Comment

Copy method #

Copy creates a new copy of StartElement.

func (e StartElement) Copy() StartElement

Copy method #

Copy creates a new copy of Directive.

func (d Directive) Copy() Directive

CopyToken function #

CopyToken returns a copy of a Token.

func CopyToken(t Token) Token

Decode method #

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

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

DecodeElement method #

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 (d *Decoder) DecodeElement(v any, start *StartElement) error

Encode method #

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 (enc *Encoder) Encode(v any) error

EncodeElement method #

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 (enc *Encoder) EncodeElement(v any, start StartElement) error

EncodeToken method #

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 (enc *Encoder) EncodeToken(t Token) error

End method #

End returns the corresponding XML end element.

func (e StartElement) End() EndElement

Error method #

func (e *TagPathError) Error() string

Error method #

func (e *SyntaxError) Error() string

Error method #

func (e UnmarshalError) Error() string

Error method #

func (e *UnsupportedTypeError) Error() string

Escape function #

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 Escape(w io.Writer, s []byte)

EscapeString method #

EscapeString writes to p the properly escaped XML equivalent of the plain text data s.

func (p *printer) EscapeString(s string)

EscapeText function #

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

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

Flush method #

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

func (enc *Encoder) Flush() error

Indent method #

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.

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

InputOffset method #

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 (d *Decoder) InputOffset() int64

InputPos method #

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 (d *Decoder) InputPos() (line int, column int)

Marshal function #

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 tag on the XMLName field, if the data is a struct - the value of the XMLName field of type [Name] - the tag of the struct field used to obtain the data - the name of the struct field used to obtain the data - the name of the marshaled type The XML element for a struct contains marshaled elements for each of the exported fields of the struct, with these exceptions: - the XMLName field, described above, is omitted. - a field with tag "-" is omitted. - a field with tag "name,attr" becomes an attribute with the given name in the XML element. - a field with tag ",attr" becomes an attribute with the field name in the XML element. - a field with tag ",chardata" is written as character data, not as an XML element. - a field with tag ",cdata" is written as character data wrapped in one or more tags, not as an XML element. - a field with tag ",innerxml" is written verbatim, not subject to the usual marshaling procedure. - a field with tag ",comment" is written as an XML comment, not subject to the usual marshaling procedure. It must not contain the "--" string within it. - a field with a tag including the "omitempty" option is omitted if the field value is empty. The empty values are false, 0, any nil pointer or interface value, and any array, slice, map, or string of length zero. - an anonymous struct field is handled as if the fields of its value were part of the outer struct. - an anonymous struct field of interface type is treated the same as having that type as its name, rather than being anonymous. - a field implementing [Marshaler] is written by calling its MarshalXML method. - a field implementing [encoding.TextMarshaler] is written by encoding the result of its MarshalText method as text. 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 Marshal(v any) ([]byte, error)

MarshalIndent function #

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.

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

NewDecoder function #

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

func NewDecoder(r io.Reader) *Decoder

NewEncoder function #

NewEncoder returns a new encoder that writes to w.

func NewEncoder(w io.Writer) *Encoder

NewTokenDecoder function #

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

func NewTokenDecoder(t TokenReader) *Decoder

RawToken method #

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 (d *Decoder) RawToken() (Token, error)

Skip method #

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 (d *Decoder) Skip() error

Token method #

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
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.

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

Unmarshal function #

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 the struct has a field of type []byte or string with tag ",innerxml", Unmarshal accumulates the raw XML nested inside the element in that field. The rest of the rules still apply. - If the struct has a field named XMLName of type Name, Unmarshal records the element name in that field. - If the XMLName field has an associated tag of the form "name" or "namespace-URL name", the XML element must have the given name (and, optionally, name space) or else Unmarshal returns an error. - If the XML element has an attribute whose name matches a struct field name with an associated tag containing ",attr" or the explicit name in a struct field tag of the form "name,attr", Unmarshal records the attribute value in that field. - If the XML element has an attribute not handled by the previous rule and the struct has a field with an associated tag containing ",any,attr", Unmarshal records the attribute value in the first such field. - If the XML element contains character data, that data is accumulated in the first struct field that has tag ",chardata". The struct field may have type []byte or string. If there is no such field, the character data is discarded. - If the XML element contains comments, they are accumulated in the first struct field that has tag ",comment". The struct field may have type []byte or string. If there is no such field, the comments are discarded. - If the XML element contains a sub-element whose name matches the prefix of a tag formatted as "a" or "a>b>c", unmarshal will descend into the XML structure looking for elements with the given names, and will map the innermost elements to that struct field. A tag starting with ">" is equivalent to one starting with the field name followed by ">". - If the XML element contains a sub-element whose name matches a struct field's XMLName tag and the struct field has no explicit name tag as per the previous rule, unmarshal maps the sub-element to that struct field. - If the XML element contains a sub-element whose name matches a field without any mode flags (",attr", ",chardata", etc), Unmarshal maps the sub-element to that struct field. - If the XML element contains a sub-element that hasn't matched any of the above rules and the struct has a field with tag ",any", unmarshal maps the sub-element to that struct field. - An anonymous struct field is handled as if the fields of its value were part of the outer struct. - A struct field with tag "-" is never unmarshaled into. 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.

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

Write method #

Write implements io.Writer

func (p *printer) Write(b []byte) (n int, err error)

WriteByte method #

WriteByte implements io.ByteWriter

func (p *printer) WriteByte(c byte) error

WriteString method #

WriteString implements io.StringWriter

func (p *printer) WriteString(s string) (n int, err error)

addFieldInfo function #

addFieldInfo adds finfo to tinfo.fields if there are no conflicts, or if conflicts arise from previous fields that were obtained from deeper embedded structures than finfo. In the latter case, the conflicting entries are dropped. A conflict occurs when the path (parent + name) to a field is itself a prefix of another path, or when two paths match exactly. It is okay for field paths to share a common, shorter prefix.

func addFieldInfo(typ reflect.Type, tinfo *typeInfo, newf *fieldInfo) error

attrval method #

func (d *Decoder) attrval() []byte

autoClose method #

If the top element on the stack is autoclosing and t is not the end tag, invent the end tag.

func (d *Decoder) autoClose(t Token) (Token, bool)

cachedWriteError method #

return the bufio Writer's cached write error

func (p *printer) cachedWriteError() error

copyValue function #

func copyValue(dst reflect.Value, src []byte) (err error)

createAttrPrefix method #

createAttrPrefix finds the name space prefix attribute to use for the given name space, defining a new prefix if necessary. It returns the prefix.

func (p *printer) createAttrPrefix(url string) string

defaultStart function #

defaultStart returns the default start element to use, given the reflect type, field info, and start template.

func defaultStart(typ reflect.Type, finfo *fieldInfo, startTemplate *StartElement) StartElement

deleteAttrPrefix method #

deleteAttrPrefix removes an attribute name space prefix.

func (p *printer) deleteAttrPrefix(prefix string)

emitCDATA function #

emitCDATA writes to w the CDATA-wrapped plain text data s. It escapes CDATA directives nested in s.

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

escapeText function #

escapeText writes to w the properly escaped XML equivalent of the plain text data s. If escapeNewline is true, newline characters will be escaped.

func escapeText(w io.Writer, s []byte, escapeNewline bool) error

getTypeInfo function #

getTypeInfo returns the typeInfo structure with details necessary for marshaling and unmarshaling typ.

func getTypeInfo(typ reflect.Type) (*typeInfo, error)

getc method #

Read a single byte. If there is no byte to read, return ok==false and leave the error in d.err. Maintain line number.

func (d *Decoder) getc() (b byte, ok bool)

indirect function #

indirect drills into interfaces and pointers, returning the pointed-at value. If it encounters a nil interface or pointer, indirect returns that nil value. This can turn into an infinite loop given a cyclic chain, but it matches the Go 1 behavior.

func indirect(vf reflect.Value) reflect.Value

isEmptyValue function #

func isEmptyValue(v reflect.Value) bool

isInCharacterRange function #

Decide whether the given rune is in the XML Character Range, per the Char production of https://www.xml.com/axml/testaxml.htm, Section 2.2 Characters.

func isInCharacterRange(r rune) (inrange bool)

isName function #

func isName(s []byte) bool

isNameByte function #

func isNameByte(c byte) bool

isNameString function #

func isNameString(s string) bool

isValidDirective function #

isValidDirective reports whether dir is a valid directive text, meaning angle brackets are matched, ignoring comments and strings.

func isValidDirective(dir Directive) bool

lookupXMLName function #

lookupXMLName returns the fieldInfo for typ's XMLName field in case it exists and has a valid xml field tag, otherwise it returns nil.

func lookupXMLName(typ reflect.Type) (xmlname *fieldInfo)

markPrefix method #

func (p *printer) markPrefix()

marshalAttr method #

marshalAttr marshals an attribute with the given name and value, adding to start.Attr.

func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value) error

marshalInterface method #

marshalInterface marshals a Marshaler interface value.

func (p *printer) marshalInterface(val Marshaler, start StartElement) error

marshalSimple method #

func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []byte, error)

marshalStruct method #

func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error

marshalTextInterface method #

marshalTextInterface marshals a TextMarshaler interface value.

func (p *printer) marshalTextInterface(val encoding.TextMarshaler, start StartElement) error

marshalValue method #

marshalValue writes one or more XML elements representing val. If val was obtained from a struct field, finfo must have its details.

func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplate *StartElement) error

mustgetc method #

Must read a single byte. If there is no byte to read, set d.err to SyntaxError("unexpected EOF") and return ok==false

func (d *Decoder) mustgetc() (b byte, ok bool)

name method #

Get name: /first(first|second) Do not set d.err if the name is missing (unless unexpected EOF is received): let the caller provide better context.

func (d *Decoder) name() (s string, ok bool)

nsname method #

Get name space name: name with a : stuck in the middle. The part before the : is the name space identifier.

func (d *Decoder) nsname() (name Name, ok bool)

pop method #

func (d *Decoder) pop() *stack

popEOF method #

Undo a pushEOF. The element must have been finished, so the EOF should be at the top of the stack.

func (d *Decoder) popEOF() bool

popElement method #

Record that we are ending an element with the given name. The name must match the record at the top of the stack, which must be a pushElement record. After popping the element, apply any undo records from the stack to restore the name translations that existed before we saw this element.

func (d *Decoder) popElement(t *EndElement) bool

popPrefix method #

func (p *printer) popPrefix()

procInst function #

procInst parses the `param="..."` or `param='...'` value out of the provided string, returning "" if not found.

func procInst(param string, s string) string

push method #

push adds parent elements to the stack and writes open tags.

func (s *parentStack) push(parents []string) error

push method #

func (d *Decoder) push(kind int) *stack

pushEOF method #

Record that after the current element is finished (that element is already pushed on the stack) Token should return EOF until popEOF is called.

func (d *Decoder) pushEOF()

pushElement method #

Record that we are starting an element with the given name.

func (d *Decoder) pushElement(name Name)

pushNs method #

Record that we are changing the value of ns[local]. The old value is url, ok.

func (d *Decoder) pushNs(local string, url string, ok bool)

rawToken method #

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

readName method #

Read a name and append its bytes to d.buf. The name is delimited by any single-byte character not valid in names. All multi-byte characters are accepted; the caller must check their validity.

func (d *Decoder) readName() (ok bool)

receiverType function #

receiverType returns the receiver type to use in an expression like "%s.MethodName".

func receiverType(val any) string

savedOffset method #

Return saved offset. If we did ungetc (nextByte >= 0), have to back up one.

func (d *Decoder) savedOffset() int

space method #

Skip spaces if any

func (d *Decoder) space()

structFieldInfo function #

structFieldInfo builds and returns a fieldInfo for f.

func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, error)

switchToReader method #

func (d *Decoder) switchToReader(r io.Reader)

syntaxError method #

Creates a SyntaxError with the current line number.

func (d *Decoder) syntaxError(msg string) error

text method #

Read plain text section (XML calls it character data). If quote >= 0, we are in a quoted string and need to find the matching quote. If cdata == true, we are in a . On failure return nil and leave the error in d.err.

func (d *Decoder) text(quote int, cdata bool) []byte

translate method #

Apply name space translation to name n. The default name space (for Space=="") applies only to element names, not to attribute names.

func (d *Decoder) translate(n *Name, isElementName bool)

trim method #

trim updates the XML context to match the longest common prefix of the stack and the given parents. A closing tag will be written for every parent popped. Passing a zero slice or nil will close all the elements.

func (s *parentStack) trim(parents []string) error

ungetc method #

Unread a single byte.

func (d *Decoder) ungetc(b byte)

unmarshal method #

Unmarshal a single XML element into val.

func (d *Decoder) unmarshal(val reflect.Value, start *StartElement, depth int) error

unmarshalAttr method #

unmarshalAttr unmarshals a single XML attribute into val.

func (d *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error

unmarshalInterface method #

unmarshalInterface unmarshals a single XML element into val. start is the opening tag of the element.

func (d *Decoder) unmarshalInterface(val Unmarshaler, start *StartElement) error

unmarshalPath method #

unmarshalPath walks down an XML structure looking for wanted paths, and calls unmarshal on them. The consumed result tells whether XML elements have been consumed from the Decoder until start's matching end element, or if it's still untouched because start is uninteresting for sv's fields.

func (d *Decoder) unmarshalPath(tinfo *typeInfo, sv reflect.Value, parents []string, start *StartElement, depth int) (consumed bool, err error)

unmarshalTextInterface method #

unmarshalTextInterface unmarshals a single XML element into val. The chardata contained in the element (but not its children) is passed to the text unmarshaler.

func (d *Decoder) unmarshalTextInterface(val encoding.TextUnmarshaler) error

value method #

value returns v's field value corresponding to finfo. It's equivalent to v.FieldByIndex(finfo.idx), but when passed initNilPointers, it initializes and dereferences pointers as necessary. When passed dontInitNilPointers and a nil pointer is reached, the function returns a zero reflect.Value.

func (finfo *fieldInfo) value(v reflect.Value, shouldInitNilPointers bool) reflect.Value

writeEnd method #

func (p *printer) writeEnd(name Name) error

writeIndent method #

func (p *printer) writeIndent(depthDelta int)

writeStart method #

writeStart writes the given start element.

func (p *printer) writeStart(start *StartElement) error

Generated with Arrow