LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2011 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 package xml |
| 6 |
| 7 // Tests specific to the context object which is included in Encoder |
| 8 // and Decoder. |
| 9 |
| 10 import ( |
| 11 "fmt" |
| 12 "io" |
| 13 "strings" |
| 14 ) |
| 15 |
| 16 type XmppErr struct { |
| 17 XMLName Name `xml:"xmpp1 error"` |
| 18 Text XmppErrTxt `xml:",any"` |
| 19 } |
| 20 |
| 21 type XmppErrTxt struct { |
| 22 XMLName Name |
| 23 } |
| 24 |
| 25 var decodectxstr = `<stream:error><blah xmlns="xmpp2"></blah></stream:error>` |
| 26 |
| 27 // Imagine we need to read from the middle of an XML document, after |
| 28 // namespaces and prefixes have already been declared. It's possible |
| 29 // to load that context into the Decoder. |
| 30 func ExampleDecoder_context() { |
| 31 datardr := strings.NewReader(decodectxstr) |
| 32 // Make a dummy start-tag with the document's namespace |
| 33 // declarations. |
| 34 nsstr := `<a xmlns="xmpp0" xmlns:stream="xmpp1">` |
| 35 nsrdr := strings.NewReader(nsstr) |
| 36 r := io.MultiReader(nsrdr, datardr) |
| 37 dec := NewDecoder(r) |
| 38 // Read and discard the start-tag; this has the side effect of |
| 39 // loading namespace prefixes. |
| 40 dec.Token() |
| 41 val := &XmppErr{} |
| 42 err := dec.Decode(val) |
| 43 if err != nil { |
| 44 fmt.Printf("Decode: %s", err) |
| 45 return |
| 46 } |
| 47 fmt.Printf("%#v", val) |
| 48 // Output: |
| 49 // &xml.XmppErr{XMLName:xml.Name{Space:"xmpp1", Local:"error"}, Text:xml
.XmppErrTxt{XMLName:xml.Name{Space:"xmpp2", Local:"blah"}}} |
| 50 } |
LEFT | RIGHT |