Documentation ¶
Index ¶
- func ParseXMLFile(fileName string, streamParser XmlStreamParser) (err error)
- func ParseXMLReader(reader io.Reader, streamParser XmlStreamParser) (err error)
- type DataFunc
- type Element
- func (elem Element) HasChild(tag TagName) bool
- func (elem *Element) OnData(fun DataFunc)
- func (elem *Element) OnEnter(fun EnterFunc)
- func (elem *Element) OnExit(fun ExitFunc)
- func (elem *Element) Parse(tag TagName, enter EnterFunc, data DataFunc, out ExitFunc) *Element
- func (elem *Element) Path() string
- func (elem *Element) Root() *Element
- func (elem *Element) Tag(names ...TagName) *Element
- type EnterFunc
- type ExitFunc
- type StreamParser
- func (parser *StreamParser) Parse(name TagName, in EnterFunc, dataFunc DataFunc, out ExitFunc) *Element
- func (parser *StreamParser) ProcessCharData(data xml.CharData) error
- func (parser StreamParser) ProcessComment(_ xml.Comment) error
- func (parser StreamParser) ProcessDirective(_ xml.Directive) error
- func (parser *StreamParser) ProcessEndElement(element xml.EndElement) (err error)
- func (parser StreamParser) ProcessProcInst(_ xml.ProcInst) error
- func (parser *StreamParser) ProcessStartElement(token xml.StartElement) (err error)
- func (parser *StreamParser) Root() *Element
- func (parser *StreamParser) Tag(names ...TagName) *Element
- type TagElementMapping
- type TagName
- type XmlStreamParser
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseXMLFile ¶
func ParseXMLFile(fileName string, streamParser XmlStreamParser) (err error)
ParseXMLFile parses XML file specified by fileName with XmlStreamParser.
func ParseXMLReader ¶
func ParseXMLReader(reader io.Reader, streamParser XmlStreamParser) (err error)
ParseXMLReader parses XML taken from io.Reader interface with XmlStreamParser.
Types ¶
type DataFunc ¶ added in v0.1.1
DataFunc declares function interface to execute when collecting data in specified Element.
type Element ¶ added in v0.1.1
type Element struct {
// contains filtered or unexported fields
}
Element type declares XML tree element data to use during parse.
func MakeElement ¶ added in v0.1.1
func MakeElement(parent *Element, tag TagName, enter EnterFunc, data DataFunc, out ExitFunc) *Element
MakeElement creates new instance of Element.
func (Element) HasChild ¶ added in v0.1.1
HasChild returns true if element has child with specified tag name.
func (*Element) OnData ¶ added in v0.1.1
OnData set function to process current element data during parse.
func (*Element) OnEnter ¶ added in v0.1.1
OnEnter set function to execute on entering current element during parse.
func (*Element) OnExit ¶ added in v0.1.1
OnExit set function to execute on exiting current element during parse.
func (*Element) Parse ¶ added in v0.1.1
Parse adds current element child specified by tag name as well as functions to execute when child enter, child data processing and child exit. Returns created child element.
func (*Element) Path ¶ added in v0.1.1
Path returns tag names chain to arrive into current element from root.
type EnterFunc ¶ added in v0.1.1
type EnterFunc func(element xml.StartElement) error
EnterFunc declares function interface to execute when entering some Element. It takes current element f.e. to extract data from tag attributes.
type ExitFunc ¶ added in v0.1.1
type ExitFunc func() error
ExitFunc declares function interface to execute when exiting some Element.
type StreamParser ¶ added in v0.1.1
type StreamParser struct {
// contains filtered or unexported fields
}
StreamParser implements libxml.XmlStreamParser. It requires parsing structure created before parse with Tag and Parse methods.
func NewParser ¶ added in v0.1.1
func NewParser(strict bool) *StreamParser
NewParser creates new parser. If strict is false, any unexpected tag will be silently ignored. If strict is true structure should define all required tag elements with parser root or some element tag method, f.e. parser.Tag("ignore", "this", "tree"). In struct mode any unexpected tag will produce parsing error.
func (*StreamParser) Parse ¶ added in v0.1.1
func (parser *StreamParser) Parse(name TagName, in EnterFunc, dataFunc DataFunc, out ExitFunc) *Element
Parse creates new root element one time with parsing functions set for enter, processing data and tag exit.
func (*StreamParser) ProcessCharData ¶ added in v0.1.1
func (parser *StreamParser) ProcessCharData(data xml.CharData) error
ProcessCharData processes tag (string) data. Processing Element should have OnData function set. Returns error from current tag processing function if encountered.
func (StreamParser) ProcessComment ¶ added in v0.1.1
func (parser StreamParser) ProcessComment(_ xml.Comment) error
ProcessComment implements comments parse. Implements XmlStreamParser.
func (StreamParser) ProcessDirective ¶ added in v0.1.1
func (parser StreamParser) ProcessDirective(_ xml.Directive) error
ProcessDirective implements directive parse. Implements XmlStreamParser.
func (*StreamParser) ProcessEndElement ¶ added in v0.1.1
func (parser *StreamParser) ProcessEndElement(element xml.EndElement) (err error)
ProcessEndElement processes tag closing. Calls ExitFunc for current element if set with OnExit method of Element. If no exit processing function set to current Element it simply returns previous element. Returns ExitFunc error if encountered.
func (StreamParser) ProcessProcInst ¶ added in v0.1.1
func (parser StreamParser) ProcessProcInst(_ xml.ProcInst) error
ProcessProcInst implements processing instruction parse. Implements XmlStreamParser.
func (*StreamParser) ProcessStartElement ¶ added in v0.1.1
func (parser *StreamParser) ProcessStartElement(token xml.StartElement) (err error)
ProcessStartElement processes token start. Implements XmlStreamParser. It checks if current Element has specified Tag child, set detected child element as current and executes OnEnter function if defined. If Strict mode set and no child defined returns with error.
func (*StreamParser) Root ¶ added in v0.1.1
func (parser *StreamParser) Root() *Element
Root returns root Element.
func (*StreamParser) Tag ¶ added in v0.1.1
func (parser *StreamParser) Tag(names ...TagName) *Element
Tag adds and returns new element with specified tag name. Parsing functions for tag should set using Element OnEnter, OnData and OnExit methods.
type TagElementMapping ¶ added in v0.1.1
TagElementMapping defines mapping of tag names to created elements. Used in Element structure to map children tag names to children elements.
type TagName ¶ added in v0.1.1
type TagName string
TagName defines string type to define XML tag name.
const ( // RootTag defines root tag name which is simply empty string. RootTag TagName = "" )
type XmlStreamParser ¶
type XmlStreamParser interface { ProcessStartElement(xml.StartElement) error ProcessEndElement(xml.EndElement) error ProcessCharData(xml.CharData) error ProcessComment(xml.Comment) error ProcessProcInst(xml.ProcInst) error ProcessDirective(xml.Directive) error }
XmlStreamParser requires implementations defines processing functions to deal with XML tree elements stream. All processing functions should return error if parsing should stopped as implemented algorithm is not suitable for incoming data.
ProcessStartElement is invoked for each opening tag. It takes xml.StartElement to process.
ProcessEndElement is invoked when tag closing. It takes xml.EndElement data to process and should return error
ProcessCharData is invoked when tag character data received. It takes xml.CharData data to process and should return error
ProcessComment is invoked when comment line encountered in XML stream. It takes xml.CharData data as byte string of comment content do not including the <!-- and --> comment markers.
ProcessComment is invoked when processing instructions like <?target inst?> taken from XML stream. It takes xml.ProcInst data.
ProcessDirective is invoked when processing directive of the form <!text> goes next in XML Sstream. It takes xml.Directive data.