xpp

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 6 Imported by: 109

README

goxpp

Build Status Coverage Status License GoDoc

The goxpp library, inspired by Java's XMLPullParser, is a lightweight wrapper for Go's standard XML Decoder, tailored for developers who need fine-grained control over XML parsing. Unlike simple unmarshaling of entire documents, this library excels in scenarios requiring manual navigation and consumption of XML elements. It provides a pull parser approach with convenience methods for effortlessly consuming whole tags, skipping elements, and more, granting a level of flexibility and control beyond what Go's standard XML decode method offers.

Overview

To begin parsing a XML document using goxpp you must pass it an io.Reader object for your document:

file, err := os.Open("path/file.xml")
parser := xpp.NewXMLPullParser(file, false, charset.NewReader)

The goxpp library decodes documents into a series of token objects:

Token Name
StartDocument
EndDocument
StartTag
EndTag
Text
Comment
ProcessingInstruction
Directive
IgnorableWhitespace

You will always start at the StartDocument token and can use the following functions to walk through a document:

Function Name Description
Next() Advance to the next Text, StartTag, EndTag, EndDocument token.
Note: skips Comment, Directive and ProcessingInstruction
NextToken() Advance to the next token regardless of type.
NextText() Advance to the next Text token.
Skip() Skip the next token.
DecodeElement(v interface{}) Decode an entire element from the current tag into a struct.
Note: must be at a StartTag token

This project is licensed under the MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CharsetReader

type CharsetReader func(charset string, input io.Reader) (io.Reader, error)

type XMLEventType

type XMLEventType int
const (
	StartDocument XMLEventType = iota
	EndDocument
	StartTag
	EndTag
	Text
	Comment
	ProcessingInstruction
	Directive
	IgnorableWhitespace // TODO: ?

)

type XMLPullParser

type XMLPullParser struct {
	// Document State
	Spaces      map[string]string
	SpacesStack []map[string]string
	BaseStack   urlStack

	// Token State
	Depth int
	Event XMLEventType
	Attrs []xml.Attr
	Name  string
	Space string
	Text  string
	// contains filtered or unexported fields
}

func NewXMLPullParser

func NewXMLPullParser(r io.Reader, strict bool, cr CharsetReader) *XMLPullParser

func (*XMLPullParser) Attribute

func (p *XMLPullParser) Attribute(name string) string

func (*XMLPullParser) DecodeElement

func (p *XMLPullParser) DecodeElement(v interface{}) error

func (*XMLPullParser) EventName

func (p *XMLPullParser) EventName(e XMLEventType) (name string)

func (*XMLPullParser) EventType

func (p *XMLPullParser) EventType(t xml.Token) (event XMLEventType)

func (*XMLPullParser) Expect

func (p *XMLPullParser) Expect(event XMLEventType, name string) (err error)

func (*XMLPullParser) ExpectAll

func (p *XMLPullParser) ExpectAll(event XMLEventType, space string, name string) (err error)

func (*XMLPullParser) IsWhitespace

func (p *XMLPullParser) IsWhitespace() bool

func (*XMLPullParser) Next

func (p *XMLPullParser) Next() (event XMLEventType, err error)

func (*XMLPullParser) NextTag

func (p *XMLPullParser) NextTag() (event XMLEventType, err error)

func (*XMLPullParser) NextText

func (p *XMLPullParser) NextText() (string, error)

func (*XMLPullParser) NextToken

func (p *XMLPullParser) NextToken() (event XMLEventType, err error)

func (*XMLPullParser) Skip

func (p *XMLPullParser) Skip() error

func (*XMLPullParser) XmlBaseResolveUrl added in v1.1.0

func (p *XMLPullParser) XmlBaseResolveUrl(u string) (*url.URL, error)

resolve the given string as a URL relative to current xml:base

Jump to

Keyboard shortcuts

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