func XMLToStruct()

in testing/xml/xmlToStruct.go [42:106]


func XMLToStruct(d *xml.Decoder, s *xml.StartElement, ignoreIndentation bool) (*XMLNode, error) {
	out := &XMLNode{}

	for {
		tok, err := d.Token()
		if err != nil {
			if err == io.EOF {
				break
			} else {
				return out, err
			}
		}

		if tok == nil {
			break
		}

		switch typed := tok.(type) {
		case xml.CharData:
			text := string(typed.Copy())
			if ignoreIndentation {
				text = strings.TrimSpace(text)
			}
			if len(text) != 0 {
				out.Text = text
			}
		case xml.StartElement:
			el := typed.Copy()
			out.Attr = el.Attr
			if out.Children == nil {
				out.Children = map[string][]*XMLNode{}
			}

			name := typed.Name.Local
			slice := out.Children[name]
			if slice == nil {
				slice = []*XMLNode{}
			}
			node, e := XMLToStruct(d, &el, ignoreIndentation)
			out.findNamespaces()
			if e != nil {
				return out, e
			}

			node.Name = typed.Name
			node.findNamespaces()

			// Add attributes onto the node
			node.Attr = el.Attr

			tempOut := *out
			// Save into a temp variable, simply because out gets squashed during
			// loop iterations
			node.parent = &tempOut
			slice = append(slice, node)
			out.Children[name] = slice
		case xml.EndElement:
			if s != nil && s.Name.Local == typed.Name.Local { // matching end token
				return out, nil
			}
			out = &XMLNode{}
		}
	}
	return out, nil
}