xpp

package module
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 7 Imported by: 0

README

goxpp

Go GoDoc

This project is a curated fork of goxpp without an AI slop. Changes from upstream:

  • Less memory allocs

    It reuses some internal structures, instead of copying it, so less allocs:

    before/after:

    BenchmarkNextTag-6  151468  8570 ns/op  5170 B/op  97 allocs/op
    BenchmarkNextTag-6  175459  6928 ns/op  4177 B/op  69 allocs/op
    

    Text field is private now and accessible via Text method, which returns copy of the parser's internal buffer as a string.

  • Export internal xml.Token

    Token() returns the current XML token in the input stream.

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

  • Allow custom xml.Decoder to be used

    NewXMLPullParser now can be called with an optional array of options. WithDecoder option configures XMLPullParser with custom xml.Decoder, like

    p := xpp.NewXMLPullParser(nil, false, cr, xpp.WithDecoder(xml.NewDecoder(r)))
    

    NewXMLPullParser will override Strict and CharsetReader of xml.Decoder.

  • Decode all HTML entities by default.

    To override it use custom xml.Decoder via xpp.WithDecoder.

  • Add AttributeNS(name, space string) string

    which returns value of an attribute named name in namespace space or empty string if not found.


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/dsh2dsh/goxpp/v2

Quick Start

import "github.com/dsh2dsh/goxpp/v2"

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

View Source
const (
	XMLnamespace = "http://www.w3.org/XML/1998/namespace"
)

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 Option

type Option func(p *XMLPullParser)

func WithDecoder

func WithDecoder(d *xml.Decoder) Option

WithDecoder configures XMLPullParser with custom xml.Decoder.

NewXMLPullParser will override xml.Decoder.Strict and xml.Decoder.CharsetReader.

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
	// contains filtered or unexported fields
}

func NewXMLPullParser

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

func (*XMLPullParser) Attribute

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

func (*XMLPullParser) AttributeNS added in v2.1.0

func (p *XMLPullParser) AttributeNS(name, space string) string

AttributeNS returns value of an attribute named name in namespace space or empty string if not found.

func (*XMLPullParser) DecodeElement

func (p *XMLPullParser) DecodeElement(v any) error

func (*XMLPullParser) EventName

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

func (*XMLPullParser) EventType

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

func (*XMLPullParser) Expect

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

func (*XMLPullParser) ExpectAll

func (p *XMLPullParser) ExpectAll(event XMLEventType, space, name string) 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() (XMLEventType, 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) Text

func (p *XMLPullParser) Text() string

Text returns text of current xml token as string.

func (*XMLPullParser) Token

func (p *XMLPullParser) Token() xml.Token

Token returns the current XML token in the input stream.

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

func (*XMLPullParser) XmlBaseResolveUrl

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

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

func (*XMLPullParser) XmlBaseResolver added in v2.0.3

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

XmlBaseResolver returns func, which resolves given string as an URL, relative to current xml:base. Can be used anytime later, event when current tag has changed.

Jump to

Keyboard shortcuts

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