Value provides an abstraction to retrieve char data value within an xml element. The method will return an error if it encounters a nested xml element instead of char data. This method should only be used to retrieve simple type or blob shape values as []byte.
()
| 104 | // The method will return an error if it encounters a nested xml element instead of char data. |
| 105 | // This method should only be used to retrieve simple type or blob shape values as []byte. |
| 106 | func (d NodeDecoder) Value() (c []byte, err error) { |
| 107 | t, e := d.Decoder.Token() |
| 108 | if e != nil { |
| 109 | return c, e |
| 110 | } |
| 111 | |
| 112 | endElement := d.StartEl.End() |
| 113 | |
| 114 | switch ev := t.(type) { |
| 115 | case xml.CharData: |
| 116 | c = ev.Copy() |
| 117 | case xml.EndElement: // end tag or self-closing |
| 118 | if ev == endElement { |
| 119 | return []byte{}, err |
| 120 | } |
| 121 | return c, fmt.Errorf("expected value for %v element, got %T type %v instead", d.StartEl.Name.Local, t, t) |
| 122 | default: |
| 123 | return c, fmt.Errorf("expected value for %v element, got %T type %v instead", d.StartEl.Name.Local, t, t) |
| 124 | } |
| 125 | |
| 126 | t, e = d.Decoder.Token() |
| 127 | if e != nil { |
| 128 | return c, e |
| 129 | } |
| 130 | |
| 131 | if ev, ok := t.(xml.EndElement); ok { |
| 132 | if ev == endElement { |
| 133 | return c, err |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return c, fmt.Errorf("expected end element %v, got %T type %v instead", endElement, t, t) |
| 138 | } |
| 139 | |
| 140 | // FetchRootElement takes in a decoder and returns the first start element within the xml body. |
| 141 | // This function is useful in fetching the start element of an XML response and ignore the |