Golang encoding/xml.Decoder.Skip() function example
package encoding/xml
Skip reads tokens until it has consumed the end element matching the most recent start element already consumed. It recurs if it encounters a start element, so it can be used to skip nested structures. It returns nil if it finds an end element matching the start element; otherwise it returns an error describing the problem.
Golang encoding/xml.Decoder.Skip() function usage example
func parseItem(p *parser) stateFunc {
t, err := p.next()
if err {
return nil
}
switch token := t.(type) {
case xml.StartElement:
// Properties
if token.Name.Space == "" && token.Name.Local == "Properties" {
if len(p.stack) == 0 {
p.dec.Skip() // <-- here
return parseItem
}
item := p.look()
if item == nil || item.HasProps {
p.dec.Skip()
return parseItem
}
item.HasProps = true
return parseProp
}
// Item
if token.Name.Space != "" || token.Name.Local != "Item" {
p.dec.Skip()
return parseItem
}
class := p.getattr(token, "", "class")
if class == nil {
p.dec.Skip()
return parseItem
}
item := &Item{
Class: class.Value,
Properties: map[string]Prop{},
HasProps: false,
Children: []*Item{},
}
p.parent(item)
p.push(item)
return parseItem
case xml.EndElement:
// Also handles root element; stack may be empty
p.pop()
return parseItem
case xml.CharData:
// ignore
default:
p.dec.Skip()
}
return parseItem
}
References :
Advertisement
Something interesting
Tutorials
+17.4k Golang : Multi threading or run two processes or more example
+9.6k Golang : How to extract video or image files from html source code
+13.1k Golang : How to get a user home directory path?
+9.5k Golang : Extract or copy items from map based on value
+7.4k Android Studio : How to detect camera, activate and capture example
+12k Golang : Decompress zlib file example
+38.1k Golang : Read a text file and replace certain words
+10.6k Golang : Bubble sort example
+10.9k Golang : Create Temporary File
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+15.7k Golang : Get checkbox or extract multipart form data value example