Token returns the next XML token in the input stream. At the end of the input stream, Token returns nil, io.EOF. Slices of bytes in the returned token data refer to the parser's internal buffer and remain valid only until the next call to Token. To acquire a copy of the bytes, call CopyToken or the
()
| 269 | // If Token encounters an unrecognized name space prefix, |
| 270 | // it uses the prefix as the Space rather than report an error. |
| 271 | func (d *Decoder) Token() (Token, error) { |
| 272 | var t Token |
| 273 | var err error |
| 274 | if d.stk != nil && d.stk.kind == stkEOF { |
| 275 | return nil, io.EOF |
| 276 | } |
| 277 | if d.nextToken != nil { |
| 278 | t = d.nextToken |
| 279 | d.nextToken = nil |
| 280 | } else { |
| 281 | if t, err = d.rawToken(); t == nil && err != nil { |
| 282 | if err == io.EOF && d.stk != nil && d.stk.kind != stkEOF { |
| 283 | err = d.syntaxError("unexpected EOF") |
| 284 | } |
| 285 | return nil, err |
| 286 | } |
| 287 | // We still have a token to process, so clear any |
| 288 | // errors (e.g. EOF) and proceed. |
| 289 | err = nil |
| 290 | } |
| 291 | if !d.Strict { |
| 292 | if t1, ok := d.autoClose(t); ok { |
| 293 | d.nextToken = t |
| 294 | t = t1 |
| 295 | } |
| 296 | } |
| 297 | switch t1 := t.(type) { |
| 298 | case StartElement: |
| 299 | // In XML name spaces, the translations listed in the |
| 300 | // attributes apply to the element name and |
| 301 | // to the other attribute names, so process |
| 302 | // the translations first. |
| 303 | for _, a := range t1.Attr { |
| 304 | if a.Name.Space == xmlnsPrefix { |
| 305 | v, ok := d.ns[a.Name.Local] |
| 306 | d.pushNs(a.Name.Local, v, ok) |
| 307 | d.ns[a.Name.Local] = a.Value |
| 308 | } |
| 309 | if a.Name.Space == "" && a.Name.Local == xmlnsPrefix { |
| 310 | // Default space for untagged names |
| 311 | v, ok := d.ns[""] |
| 312 | d.pushNs("", v, ok) |
| 313 | d.ns[""] = a.Value |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | d.pushElement(t1.Name) |
| 318 | d.translate(&t1.Name, true) |
| 319 | for i := range t1.Attr { |
| 320 | d.translate(&t1.Attr[i].Name, false) |
| 321 | } |
| 322 | t = t1 |
| 323 | |
| 324 | case EndElement: |
| 325 | if !d.popElement(&t1) { |
| 326 | return nil, d.err |
| 327 | } |
| 328 | t = t1 |
no test coverage detected