KubFly SDK for Go / codec/xml
package xml
import "kubfly.io/sdk/codec/xml"
Package xml is a streaming parser of XML, strict and without a tree, and its generator — io.kubfly.sdk.codec.xml of the Java SDK: Next reads the document event by event, a reader takes what it needs and skips the rest.
parser, err := xml.NewParser(body, codec.DefaultLimits)
event, err := parser.Next() // EventStart ListBucketResult
for event, err = parser.Next(); err == nil && event == xml.EventStart; event, err = parser.Next() {
switch parser.Name() {
case "Contents":
err = contents(parser)
case "IsTruncated":
text, err = parser.ElementText()
default:
err = parser.Skip()
}
}
It takes a subset of XML 1.0 — what servers answer with: elements, attributes, text, the five predefined entities and character references, CDATA sections; the XML declaration (version 1.0, UTF-8) and comments are read past. A document type and processing instructions are failures: there are no entities of the document, so no external entity and no expansion of entities can ever be read (XXE, "billion laughs"). The text is UTF-8, checked strictly; line ends become \n, white space in attribute values a space, as XML says. Names are not split by namespaces: s3:Key is a name; xmlns is an attribute. Every failure — not XML of this subset, beyond the codec.Limits — is a *codec.Error with the offset, the same as in every SDK (sdk/spec/codec/xml.tsv), and every call after it gives it again. Not safe for concurrent use.
Functions
func WriteAttribute
func WriteAttribute(buffer []byte, offset int, name, value string) int
WriteAttribute writes an attribute: ` name="value"`, the value escaped.
func WriteDeclaration
func WriteDeclaration(buffer []byte, offset int) int
WriteDeclaration writes the declaration: <?xml version="1.0" encoding="UTF-8"?>. The generator — the Write… functions, io.kubfly.sdk.codec.xml.XmlGenerator of the Java SDK — writes tags, attributes and text straight into a buffer at an offset and gives the offset after them, nothing allocated; a nil buffer writes nothing and counts the bytes. The XML that Parser reads, the same in every SDK (sdk/spec/codec/xml-generator.tsv): UTF-8; in a text & < > as their entities and a carriage return as 
 (a parser would turn it into a line feed), the rest as it is; in a value of an attribute, in double quotes, also " as " and a tab, a line feed and a carriage return as their references (a parser would turn them into spaces). A character XML does not allow — a control character, U+FFFE, U+FFFF — and a byte that is not UTF-8 are U+FFFD. A name that is not one of XML 1.0 is a fault of the code that writes: a panic.
func WriteEmptyTagEnd
func WriteEmptyTagEnd(buffer []byte, offset int) int
WriteEmptyTagEnd writes the end of a start tag of an empty element: />.
func WriteEndTag
func WriteEndTag(buffer []byte, offset int, name string) int
WriteEndTag writes an end tag: </name>.
func WriteStartTag
func WriteStartTag(buffer []byte, offset int, name string) int
WriteStartTag writes the start of a start tag: <name; its attributes follow, then its end.
func WriteStartTagEnd
func WriteStartTagEnd(buffer []byte, offset int) int
WriteStartTagEnd writes the end of a start tag: >.
func WriteText
func WriteText(buffer []byte, offset int, text string) int
WriteText writes a text, escaped.
Types
type Buffer
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is an XML document written with the generator into a buffer of its own that grows — XmlBuffer of the Java SDK: the end tags by the elements open, an element without content as <name/>, the structure checked as it goes (an attribute after the content, an attribute repeated, a text outside the element of the document, a second element of the document, an end with nothing open panic: they are faults of the code that writes). Kept and Reset, it writes one document after another without allocating. Not safe for concurrent use. The zero value is ready.
func (*Buffer) Attribute
func (b *Buffer) Attribute(name, value string)
Attribute writes an attribute of the element just opened.
func (*Buffer) Bytes
func (b *Buffer) Bytes() []byte
Bytes are the bytes written, valid until the next write.
func (*Buffer) Declaration
func (b *Buffer) Declaration()
Declaration writes the declaration, first in the document.
func (*Buffer) EndElement
func (b *Buffer) EndElement()
EndElement closes the element open: /> when it has no content, its end tag otherwise.
func (*Buffer) Len
func (b *Buffer) Len() int
Len is the number of bytes written.
func (*Buffer) Reset
func (b *Buffer) Reset()
Reset forgets what was written, keeps the buffer.
func (*Buffer) StartElement
func (b *Buffer) StartElement(name string)
StartElement opens an element: its start tag, open for its attributes.
func (*Buffer) String
func (b *Buffer) String() string
String is the text written.
func (*Buffer) Text
func (b *Buffer) Text(value string)
Text writes a text in the element open.
type Event
type Event uint8
Event is what Next read.
const ( // EventNone is before the first event. EventNone Event = iota // EventStart is a start tag: Name and the attributes; an empty element gives its EventEnd next. EventStart // EventEnd is an end tag, or the end of an empty element: Name. EventEnd // EventText is the text between two tags, Text: character data, references and CDATA sections as one — // comments do not divide it; never empty. EventText // EventEndDocument is after the root element: nothing more is read. EventEndDocument )
The events.
func (Event) String
func (e Event) String() string
String is the name of the event, as XmlEvent of the Java SDK: START, END, TEXT, END_DOCUMENT.
type Parser
type Parser struct {
// contains filtered or unexported fields
}
Parser is a parser of one document.
func NewParser
func NewParser(document []byte, limits codec.Limits) (*Parser, error)
NewParser is a parser of a document, before its first event: an error if the document is longer than the limit. The document is UTF-8; it is not copied and is not to be changed while it is read. Limits that are not positive are a programming error, a panic.
func (*Parser) Attribute
func (p *Parser) Attribute(name string) (string, bool)
Attribute is the value of an attribute of the EventStart read last, by its name; false if there is none.
func (*Parser) AttributeCount
func (p *Parser) AttributeCount() int
AttributeCount is the count of the attributes of the EventStart read last.
func (*Parser) AttributeName
func (p *Parser) AttributeName(index int) string
AttributeName is the name of an attribute of the EventStart read last, in the order of the document.
func (*Parser) AttributeValue
func (p *Parser) AttributeValue(index int) string
AttributeValue is the value of an attribute of the EventStart read last, in the order of the document.
func (*Parser) Depth
func (p *Parser) Depth() int
Depth is the depth of the event read last: of an EventStart, with its element (the root is 1); of an EventEnd, without it.
func (*Parser) ElementText
func (p *Parser) ElementText() (string, error)
ElementText reads the text of the element of the EventStart read last, up to its EventEnd: "" for an element without one; an error if the element has an element inside, or the document fails. Not at a start, a panic: a programming error.
func (*Parser) Event
func (p *Parser) Event() Event
Event is the event read last: EventNone before the first.
func (*Parser) Name
func (p *Parser) Name() string
Name is the name of the element of the EventStart or the EventEnd read last.
func (*Parser) Next
func (p *Parser) Next() (event Event, err error)
Next reads the next event: the first is the EventStart of the root element, the last EventEndDocument, given again on every call after it. An error if the document is not XML of the subset or is beyond the limits.
func (*Parser) Offset
func (p *Parser) Offset() int64
Offset is the offset of the event read last: the < of its tag, the first byte of its text.
func (*Parser) Skip
func (p *Parser) Skip() error
Skip reads past the element of the EventStart read last, up to its EventEnd; an error if the document fails. Not at a start, a panic: a programming error.
func (*Parser) Text
func (p *Parser) Text() string
Text is the text of the EventText read last.