xpp

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 6 Imported by: 132

README

goxpp

Build Status codecov License Go Reference

A lightweight XML Pull Parser for Go, inspired by Java's XMLPullParser. It provides fine-grained control over XML parsing with a simple, intuitive API.

Features

  • Pull-based parsing for fine-grained document control
  • Efficient navigation and element skipping
  • Simple, idiomatic Go API

Installation

go get github.com/mmcdole/goxpp

Quick Start

import "github.com/mmcdole/goxpp"

// Parse RSS feed
file, _ := os.Open("feed.rss")
p := xpp.NewXMLPullParser(file, false, nil)

// Find channel element
for tok, err := p.NextTag(); tok != xpp.EndDocument; tok, err = p.NextTag() {
    if err != nil {
        return err
    }
    if tok == xpp.StartTag && p.Name == "channel" {
        // Process channel contents
        for tok, err = p.NextTag(); tok != xpp.EndTag; tok, err = p.NextTag() {
            if err != nil {
                return err
            }
            if tok == xpp.StartTag {
                switch p.Name {
                case "title":
                    title, _ := p.NextText()
                    fmt.Printf("Feed: %s\n", title)
                case "item":
                    // Get item title and skip rest
                    p.NextTag()
                    title, _ := p.NextText()
                    fmt.Printf("Item: %s\n", title)
                    p.Skip()
                default:
                    p.Skip()
                }
            }
        }
        break
    }
}

Token Types

  • StartDocument, EndDocument
  • StartTag, EndTag
  • Text, Comment
  • ProcessingInstruction, Directive
  • IgnorableWhitespace

Documentation

For detailed documentation and examples, visit pkg.go.dev.

License

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 NewXMLPullParserWithDecoder added in v1.2.0

func NewXMLPullParserWithDecoder(d *xml.Decoder) *XMLPullParser

NewXMLPullParserWithDecoder creates a new XMLPullParser with a custom decoder

func (*XMLPullParser) Attribute

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

Attribute returns the value of the named attribute, preferring an un-namespaced attribute over foreign-namespaced ones sharing the local name (a document-order first match let e.g. an earlier xlink:href shadow the plain href). A namespaced attribute is still returned when no plain one exists.

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

Skip consumes tokens until the end tag matching the element the parser is currently positioned on. It is iterative (a depth counter rather than recursion) so deeply nested input can't overflow the goroutine stack, and it bails on EndDocument instead of looping forever on a truncated stream.

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