xmlvisitor

package module
v0.0.0-...-388de8f Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2018 License: GPL-3.0 Imports: 4 Imported by: 3

README

Introduction

This library allows for efficient traversal of XML using the visitor pattern within Go. It also tracks state in order to provide some convenience functions.

Usage

Implement an interface and pass a reader resource along with an instance of your type to the parser.

Interfaces

SimpleXmlVisitor
  • HandleStart(tagName *string, attrp *map[string]string, xp *XmlParser) error

    Receives start nodes.

  • HandleEnd(tagName *string, xp *XmlParser) error

    Receives end nodes.

  • HandleValue(tagName *string, data *string, xp *XmlParser) error

    Return values found between start and end nodes where other subnodes weren't present. This is a convenience wrapper that intelligently manages character-data.

ExtendedXmlVisitor
  • HandleStart(tagName *string, attrp *map[string]string, xp *XmlParser) error

    Receives start nodes.

  • HandleEnd(tagName *string, xp *XmlParser) error

    Receives end nodes.

  • HandleValue(tagName *string, data *string, xp *XmlParser) error

    Receives values found between start and end nodes where other subnodes weren't present.

  • HandleCharData(data *string, xp *XmlParser) error

    Receives content ("character data") not found within a tag.

  • HandleComment(comment *string, xp *XmlParser) error

    Receives comment text.

  • HandleProcessingInstruction(target *string, instruction *string, xp *XmlParser) error

    Receives processing instructions (e.g. ).

  • HandleDirective(directive *string, xp *XmlParser) error

    Receives directives (e.g. ).

Configuration

  • SetDoReportMarginCharData(value bool)

    Default: false

    Trigger on the character data that appears between adjacent open tags or adjacent close tags.

  • SetDoAutoTrimCharData(value bool)

    Default: true

    Remove empty space from the ends of character data. This also affects the values that we derive from character data (received by HandleValue()).

Other Conveniences

The visitor callbacks will have an instance of the XmlParser passed-in as an argument. This can be used to access additional functionality.

Last Node State

Calling GetLastState() on the XmlParser object will return the last [useful] type of token that was encountered. It will be equal to one of the XmlPart constants:

  • xmlvisitor.XmlPartStartTag
  • xmlvisitor.XmlPartEndTag
  • xmlvisitor.XmlPartCharData

Stack

The visitor callbacks have access to the current stack of nodes using NodeStack(). This returns an instance of the Stack type. See stack.go for further detail.

Notes

  • There is no specific handling of namespaces. This is left as an exercise to the implementor. This library merely provides a simplification of the low-level tokenizer.

Example

See tests.

Documentation

Index

Constants

View Source
const (
	XmlPart_Initial = iota
	XmlPartStartTag = iota
	XmlPartEndTag   = iota
	XmlPartCharData = iota
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ExtendedXmlVisitor

type ExtendedXmlVisitor interface {
	// Content that comes before one open/close tag and an adjacent one: either
	// the useless whitespace between two open adjacent tags or two close
	// adjacent tags or a tangible/empty value between an open and close tag.
	HandleCharData(data string, xp *XmlParser) error

	// Example:
	//
	// <!-- Comment -->
	HandleComment(comment string, xp *XmlParser) error

	// Example:
	//
	// <?xml version="1.0" encoding="UTF-8"?>
	HandleProcessingInstruction(target string, instruction string, xp *XmlParser) error

	// Example:
	//
	// <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	//   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	//
	// <![CDATA[Some text here.]]>
	HandleDirective(directive string, xp *XmlParser) error

	SimpleXmlVisitor
}

type SimpleXmlVisitor

type SimpleXmlVisitor interface {
	// The content identifier next to the left angle brack.
	HandleStart(tagName string, attr map[string]string, xp *XmlParser) error

	// The content identifier next to the left angle brack.
	HandleEnd(tagName string, xp *XmlParser) error

	// Return the value that was found between two tags not having any child
	// nodes.
	HandleValue(tagName string, data string, xp *XmlParser) error
}

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

func (*Stack) PeekFromEnd

func (s *Stack) PeekFromEnd(i int) interface{}

Return the value of the (n-i) node (n being the last node).

type XmlParser

type XmlParser struct {
	// contains filtered or unexported fields
}

func NewXmlParser

func NewXmlParser(r io.Reader, visitor XmlVisitor) *XmlParser

Create parser.

func (*XmlParser) GetLastLastState

func (xp *XmlParser) GetLastLastState() int

func (*XmlParser) GetLastState

func (xp *XmlParser) GetLastState() int

func (*XmlParser) LastState

func (xp *XmlParser) LastState() int

func (*XmlParser) LastStateName

func (xp *XmlParser) LastStateName() string

func (*XmlParser) NodeStack

func (xp *XmlParser) NodeStack() *Stack

func (*XmlParser) Parse

func (xp *XmlParser) Parse() (err error)

Run the parse with a minimal memory footprint.

func (*XmlParser) PushLastState

func (xp *XmlParser) PushLastState(lastState int)

func (*XmlParser) SetDoAutoTrimCharData

func (xp *XmlParser) SetDoAutoTrimCharData(value bool)

func (*XmlParser) SetDoReportMarginCharData

func (xp *XmlParser) SetDoReportMarginCharData(value bool)

type XmlVisitor

type XmlVisitor interface{}

Jump to

Keyboard shortcuts

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