Documentation
¶
Overview ¶
The exml package provides an intuitive event based XML parsing API which sits on top of a standard Go xml.Decoder, greatly simplifying the parsing code while retaining the raw speed and low memory overhead of the underlying stream engine, regardless of the size of the input. The package takes care of the complex tasks of maintaining contexts between event handlers allowing you to concentrate on dealing with the actual structure of the XML document.
Example ¶
reader := strings.NewReader(EXAMPLE)
decoder := NewDecoder(reader)
addressBook := AddressBook{}
decoder.On("address-book", func(attrs Attrs) {
addressBook.Name, _ = attrs.Get("name")
decoder.On("contact", func(attrs Attrs) {
contact := &Contact{}
addressBook.Contacts = append(addressBook.Contacts, contact)
decoder.OnTextOf("first-name", func(text CharData) {
contact.FirstName = string(text)
})
decoder.OnTextOf("last-name", func(text CharData) {
contact.LastName = string(text)
})
decoder.OnTextOf("address", func(text CharData) {
contact.Address = string(text)
})
})
})
decoder.Run()
fmt.Printf("Address book: %s\n", addressBook.Name)
for _, c := range addressBook.Contacts {
fmt.Println("-", c.FirstName, c.LastName, "@", c.Address)
}
Output: Address book: homies - Tim Cook @ Cupertino - Steve Ballmer @ Redmond - Mark Zuckerberg @ Menlo Park
Index ¶
- type Attrs
- func (a Attrs) Get(name string) (string, bool)
- func (a Attrs) GetBool(name string, fallback bool) bool
- func (a Attrs) GetFloat(name string, bitsize int, fallback float64) float64
- func (a Attrs) GetInt(name string, base int, bitsize int, fallback int64) int64
- func (a Attrs) GetString(name string, fallback string) string
- func (a Attrs) GetUInt(name string, base int, bitsize int, fallback uint64) uint64
- type CharData
- type Decoder
- type ErrorCallback
- type TagCallback
- type TextCallback
- func Append(a *[]string) TextCallback
- func AppendBool(a *[]bool, fallback bool) TextCallback
- func AppendFloat(a *[]float64, bitsize int, fallback float64) TextCallback
- func AppendInt(a *[]int64, base int, bitsize int, fallback int64) TextCallback
- func AppendUInt(a *[]uint64, base int, bitsize int, fallback uint64) TextCallback
- func Assign(v *string) TextCallback
- func AssignBool(v *bool, fallback bool) TextCallback
- func AssignFloat(v *float64, bitsize int, fallback float64) TextCallback
- func AssignInt(v *int64, base int, bitsize int, fallback int64) TextCallback
- func AssignUInt(v *uint64, base int, bitsize int, fallback uint64) TextCallback
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attrs ¶
func (Attrs) Get ¶
Get returns the value of the requested attribute and true when the attributes exists, or an empty string and false when it doesn't.
func (Attrs) GetBool ¶
GetBool returns the value of the requested attribute as a bool when it exists or the passed fallback value when it doesn't. The accepted values corresponds to the one accepted by the strconv.ParseBool() function.
func (Attrs) GetFloat ¶
GetFloat returns the value of the requested attribute as a float when it exists or the passed fallback value when it doesn't. The additional parameters and the accepted values corresponds to the one accepted by the strconv.ParseFloat() function.
func (Attrs) GetInt ¶
GetInt returns the value of the requested attribute as an int when it exists or the passed fallback value when it doesn't. The additional parameters and the accepted values corresponds to the one accepted by the strconv.ParseInt() function.
func (Attrs) GetString ¶
GetString returns the value of the requested attribute when it exists or the passed fallback value when it doesn't.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder wraps an xml.Decoder and maintains the various states between the encountered XML nodes during parsing.
func NewCustomDecoder ¶
NewCustomDecoder creates a new exml parser reading from the passed xml.Decoder which is useful when you need to configure the underlying decoder, when you need to handle non-UTF8 xml documents for example.
func NewDecoder ¶
NewDecoder creates a new exml parser reading from r.
func (*Decoder) On ¶
func (d *Decoder) On(path string, callback TagCallback)
On registers a handler for a single tag or for a path.
func (*Decoder) OnError ¶
func (d *Decoder) OnError(handler ErrorCallback)
OnError registers a global error handler which will be called whenever the underlying xml.Decoder reports an error.
func (*Decoder) OnText ¶
func (d *Decoder) OnText(callback TextCallback)
OnText registers a handler for the text content of the current tag.
func (*Decoder) OnTextOf ¶
func (d *Decoder) OnTextOf(path string, callback TextCallback)
OnTextOf registers a handler for the text content of a single tag or for the text content at a certain path.
type ErrorCallback ¶
type ErrorCallback func(error)
type TagCallback ¶
type TagCallback func(Attrs)
type TextCallback ¶
type TextCallback func(CharData)
func Append ¶
func Append(a *[]string) TextCallback
Append is a helper function which returns a text callback that appends the text content of the current tag to the passed strings slice pointer.
func AppendBool ¶
func AppendBool(a *[]bool, fallback bool) TextCallback
AppendBool is a helper function which returns a text callback that appends the text content of the current tag parsed as a bool to the passed slice pointer. The accepted text values correspond to the one accepted by the strconv.ParseBool() function. The fallback parameter value is used when the parsing of the text content fails.
func AppendFloat ¶
func AppendFloat(a *[]float64, bitsize int, fallback float64) TextCallback
AppendFloat is a helper function which returns a text callback that appends the text content of the current tag parsed as a float to the passed slice pointer. The accepted text values correspond to the one accepted by the strconv.ParseFloat() function. The fallback parameter value is used when the parsing of the text content fails.
func AppendInt ¶
func AppendInt(a *[]int64, base int, bitsize int, fallback int64) TextCallback
AppendInt is a helper function which returns a text callback that appends the text content of the current tag parsed as an int to the passed slice pointer. The accepted text values correspond to the one accepted by the strconv.ParseInt() function. The fallback parameter value is used when the parsing of the text content fails.
func AppendUInt ¶
func AppendUInt(a *[]uint64, base int, bitsize int, fallback uint64) TextCallback
AppendUInt is a helper function which returns a text callback that appends the text content of the current tag parsed as an uint to the passed slice pointer. The accepted text values correspond to the one accepted by the strconv.ParseUint() function. The fallback parameter value is used when the parsing of the text content fails.
func Assign ¶
func Assign(v *string) TextCallback
Assign is a helper function which returns a text callback that assigns the text content of the current tag to the passed variable pointer.
func AssignBool ¶
func AssignBool(v *bool, fallback bool) TextCallback
AssignBool is a helper function which returns a text callback that assigns the text content of the current tag parsed as a bool to the passed variable pointer. The accepted text values correspond to the one accepted by the strconv.ParseBool() function. The fallback parameter value is used when the parsing of the text content fails.
func AssignFloat ¶
func AssignFloat(v *float64, bitsize int, fallback float64) TextCallback
AssignFloat is a helper function which returns a text callback that assigns the text content of the current tag parsed as a float to the passed variable pointer. The accepted text values correspond to the one accepted by the strconv.ParseFloat() function. The fallback parameter value is used when the parsing of the text content fails.
func AssignInt ¶
func AssignInt(v *int64, base int, bitsize int, fallback int64) TextCallback
AssignInt is a helper function which returns a text callback that assigns the text content of the current tag parsed as an int to the passed variable pointer. The accepted text values correspond to the one accepted by the strconv.ParseInt() function. The fallback parameter value is used when the parsing of the text content fails.
func AssignUInt ¶
func AssignUInt(v *uint64, base int, bitsize int, fallback uint64) TextCallback
AssignUInt is a helper function which returns a text callback that assigns the text content of the current tag parsed as an uint to the passed variable pointer. The accepted text values correspond to the one accepted by the strconv.ParseUint() function. The fallback parameter value is used when the parsing of the text content fails.
