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 :

http://golang.org/pkg/encoding/xml/#Decoder.Skip

https://github.com/RobloxAPI/rbxml/blob/master/decoder.go

Advertisement