GetElement looks for the given tag name at the current level, and returns the element if found, and skipping over non-matching elements. Returns an error if the node is not found, or if an error occurs while walking the document.
(name string)
| 80 | // skipping over non-matching elements. Returns an error if the node is not found, or if an error occurs while walking |
| 81 | // the document. |
| 82 | func (d NodeDecoder) GetElement(name string) (t xml.StartElement, err error) { |
| 83 | for { |
| 84 | token, done, err := d.Token() |
| 85 | if err != nil { |
| 86 | return t, err |
| 87 | } |
| 88 | if done { |
| 89 | return t, fmt.Errorf("%s node not found", name) |
| 90 | } |
| 91 | switch { |
| 92 | case strings.EqualFold(name, token.Name.Local): |
| 93 | return token, nil |
| 94 | default: |
| 95 | err = d.Decoder.Skip() |
| 96 | if err != nil { |
| 97 | return t, err |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Value provides an abstraction to retrieve char data value within an xml element. |
| 104 | // The method will return an error if it encounters a nested xml element instead of char data. |