本文整理匯總了Golang中encoding/xml.Encoder.Encode方法的典型用法代碼示例。如果您正苦於以下問題:Golang Encoder.Encode方法的具體用法?Golang Encoder.Encode怎麽用?Golang Encoder.Encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類encoding/xml.Encoder
的用法示例。
在下文中一共展示了Encoder.Encode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: marshalInnerXML
func (o *OSM) marshalInnerXML(e *xml.Encoder) error {
if o == nil {
return nil
}
if err := e.Encode(o.Bounds); err != nil {
return err
}
if err := e.Encode(o.Nodes); err != nil {
return err
}
if err := e.Encode(o.Ways); err != nil {
return err
}
if err := e.Encode(o.Relations); err != nil {
return err
}
if err := e.Encode(o.Changesets); err != nil {
return err
}
return nil
}
示例2: MarshalXML
func (node *Optiongroup) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
var err error
startElt := xml.StartElement{Name: xml.Name{Local: "optiongroup"}}
err = e.EncodeToken(startElt)
if err != nil {
return err
}
err = marshalShortDescription("shortdescription", e, node.ShortDescription)
if err != nil {
return err
}
err = e.Encode(node.Classoption)
if err != nil {
return err
}
err = e.EncodeToken(xml.EndElement{Name: startElt.Name})
if err != nil {
return err
}
return nil
}
示例3: MarshalXML
func (people People) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
name := xml.Name{Space: "", Local: "People"}
enc.EncodeToken(xml.StartElement{Name: name})
for _, person := range people {
enc.Encode(person)
}
enc.EncodeToken(xml.EndElement{Name: name})
return nil
}
示例4: MarshalXML
// MarshalXML marshals only the fields needed for creating/updating adjustments
// with the recurly API.
func (a Adjustment) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
am := adjustmentMarshaler{
Description: a.Description,
AccountingCode: a.AccountingCode,
UnitAmountInCents: a.UnitAmountInCents,
Quantity: a.Quantity,
Currency: a.Currency,
TaxCode: a.TaxCode,
TaxExempt: a.TaxExempt,
}
return e.Encode(am)
}
示例5: subscribe
func subscribe(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, fullJID string) error {
var bareJID string
if barePosition := strings.Index(fullJID, "/"); barePosition < 0 {
return fmt.Errorf("Can't split JID %s", fullJID)
} else {
bareJID = fullJID[:barePosition]
}
var subscribe struct {
XMLName xml.Name `xml:"jabber:client iq"`
Type string `xml:"type,attr"`
To string `xml:"to,attr"`
ID string `xml:"id,attr"`
Subscribe struct {
XMLName xml.Name `xml:"google:push subscribe"`
Item struct {
XMLName xml.Name `xml:"item"`
Channel string `xml:"channel,attr"`
From string `xml:"from,attr"`
}
}
}
subscribe.Type = "set"
subscribe.To = bareJID
subscribe.ID = "3"
subscribe.Subscribe.Item.Channel = "cloudprint.google.com"
subscribe.Subscribe.Item.From = "cloudprint.google.com"
if err := xmlEncoder.Encode(&subscribe); err != nil {
return fmt.Errorf("XMPP subscription request failed: %s", err)
}
var subscription struct {
XMLName xml.Name `xml:"jabber:client iq"`
To string `xml:"to,attr"`
From string `xml:"from,attr"`
}
if err := xmlDecoder.Decode(&subscription); err != nil {
return fmt.Errorf("XMPP subscription response invalid: %s", err)
} else if fullJID != subscription.To || bareJID != subscription.From {
return errors.New("XMPP subscription failed")
}
return nil
}
示例6: WrapList
func WrapList(e *xml.Encoder, listName xml.Name, list interface{}) error {
listVal := reflect.ValueOf(list)
token := xml.StartElement{
Name: listName,
}
err := e.EncodeToken(token)
if err != nil {
return err
}
count := listVal.Len()
for index := 0; index < count; index++ {
err = e.Encode(listVal.Index(index).Interface())
if err != nil {
return err
}
}
return e.EncodeToken(token.End())
}
示例7: MarshalXML
func (t *Tag) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name = t.Name
start.Attr = t.Attr
e.EncodeToken(start)
for _, v := range t.Children {
switch v.(type) {
case *Tag:
child := v.(*Tag)
if err := e.Encode(child); err != nil {
return err
}
case xml.CharData:
e.EncodeToken(v.(xml.CharData))
case xml.Comment:
e.EncodeToken(v.(xml.Comment))
}
}
e.EncodeToken(start.End())
return nil
}
示例8: MarshalXML
func (x *domm) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
start.Name.Local = x.model.Name()
for k, v := range x.model.AttrAsMap() {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: k}, Value: v})
}
e.EncodeToken(start)
for _, _l := range x.model.Children() {
switch l := _l.(type) {
case dom.Element:
child := &domm{}
child.model = l
e.Encode(child)
case dom.Text:
e.EncodeToken(xml.CharData(l.Data()))
default:
halt.As(100, reflect.TypeOf(l))
}
}
e.EncodeToken(start.End())
return
}
示例9: MarshalXML
func (fc *FeatureContainer) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start = xml.StartElement{Name: xml.Name{Local: "stream:features"}}
if err := e.EncodeToken(start); err != nil {
return err
}
fs := make([]Feature, 0, len(fc.features))
for _, v := range fc.features {
fs = append(fs, v)
}
if err := e.Encode(fs); err != nil {
return err
}
if err := e.EncodeToken(start.End()); err != nil {
return err
}
return e.Flush()
}
示例10: marshalTwiml
func marshalTwiml(t twimlResponse, e *xml.Encoder, start *xml.StartElement) error {
return e.Encode(t.getContents())
}
示例11: xmppHandshake
func xmppHandshake(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, domain, proxyName string) (string, error) {
handshake := xml.StartElement{
Name: xml.Name{"jabber:client", "stream:stream"},
Attr: []xml.Attr{
xml.Attr{xml.Name{Local: "to"}, domain},
xml.Attr{xml.Name{Local: "xml:lang"}, "en"},
xml.Attr{xml.Name{Local: "version"}, "1.0"},
xml.Attr{xml.Name{Local: "xmlns:stream"}, "http://etherx.jabber.org/streams"},
},
}
if err := xmlEncoder.EncodeToken(handshake); err != nil {
return "", fmt.Errorf("Failed to write SASL handshake: %s", err)
}
if err := xmlEncoder.Flush(); err != nil {
return "", fmt.Errorf("Failed to flush encoding stream: %s", err)
}
if startElement, err := readStartElement(xmlDecoder); err != nil {
return "", err
} else if startElement.Name.Space != "http://etherx.jabber.org/streams" ||
startElement.Name.Local != "stream" {
return "", fmt.Errorf("Read unexpected XMPP XML stanza: %s", startElement.Name.Local)
}
var features struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
Bind *struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
}
Session *struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-session session"`
}
}
if err := xmlDecoder.Decode(&features); err != nil {
return "", fmt.Errorf("Read unexpected XMPP XML element: %s", err)
} else if features.Bind == nil || features.Session == nil {
return "", errors.New("XMPP bind or session missing from handshake")
}
var resource struct {
XMLName xml.Name `xml:"jabber:client iq"`
Type string `xml:"type,attr"`
ID string `xml:"id,attr"`
Bind struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
Resource struct {
XMLName xml.Name `xml:"resource"`
ResourceName string `xml:",chardata"`
}
}
}
resource.Type = "set"
resource.ID = "0"
resource.Bind.Resource.ResourceName = proxyName
if err := xmlEncoder.Encode(&resource); err != nil {
return "", fmt.Errorf("Failed to set resource during XMPP handshake: %s", err)
}
var jid struct {
XMLName xml.Name `xml:"jabber:client iq"`
Bind *struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
JID string `xml:"jid"`
}
}
if err := xmlDecoder.Decode(&jid); err != nil {
return "", err
} else if jid.Bind == nil || jid.Bind.JID == "" {
return "", errors.New("Received unexpected XML element during XMPP handshake")
}
fullJID := jid.Bind.JID
var session struct {
XMLName xml.Name `xml:"jabber:client iq"`
Type string `xml:"type,attr"`
ID string `xml:"id,attr"`
Session struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-session session"`
}
}
session.Type = "set"
session.ID = "1"
if err := xmlEncoder.Encode(&session); err != nil {
return "", fmt.Errorf("Failed to complete XMPP handshake: %s", err)
}
var done struct {
XMLName xml.Name `xml:"jabber:client iq"`
ID string `xml:"id,attr"`
}
if err := xmlDecoder.Decode(&done); err != nil {
return "", err
} else if done.ID != "1" {
return "", errors.New("Received unexpected result at end of XMPP handshake")
}
return fullJID, nil
}
示例12: saslHandshake
func saslHandshake(xmlEncoder *xml.Encoder, xmlDecoder *xml.Decoder, domain, user, accessToken string) error {
handshake := xml.StartElement{
Name: xml.Name{"jabber:client", "stream:stream"},
Attr: []xml.Attr{
xml.Attr{xml.Name{Local: "to"}, domain},
xml.Attr{xml.Name{Local: "xml:lang"}, "en"},
xml.Attr{xml.Name{Local: "version"}, "1.0"},
xml.Attr{xml.Name{Local: "xmlns:stream"}, "http://etherx.jabber.org/streams"},
},
}
if err := xmlEncoder.EncodeToken(handshake); err != nil {
return fmt.Errorf("Failed to write SASL handshake: %s", err)
}
if err := xmlEncoder.Flush(); err != nil {
return fmt.Errorf("Failed to flush encoding stream: %s", err)
}
if startElement, err := readStartElement(xmlDecoder); err != nil {
return err
} else if startElement.Name.Space != "http://etherx.jabber.org/streams" ||
startElement.Name.Local != "stream" {
return fmt.Errorf("Read unexpected SASL XML stanza: %s", startElement.Name.Local)
}
var features struct {
XMLName xml.Name `xml:"http://etherx.jabber.org/streams features"`
Mechanisms *struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl mechanisms"`
}
}
if err := xmlDecoder.Decode(&features); err != nil {
return fmt.Errorf("Read unexpected SASL XML element: %s", err)
} else if features.Mechanisms == nil {
return errors.New("SASL mechanisms missing from handshake")
}
credential := base64.StdEncoding.EncodeToString([]byte("\x00" + user + "\x00" + accessToken))
var auth struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl auth"`
Mechanism string `xml:"mechanism,attr"`
Service string `xml:"auth:service,attr"`
Allow string `xml:"auth:allow-generated-jid,attr"`
FullBind string `xml:"auth:client-uses-full-bind-result,attr"`
XMLNS string `xml:"xmlns:auth,attr"`
Credential string `xml:",chardata"`
}
auth.Mechanism = "X-OAUTH2"
auth.Service = "chromiumsync"
auth.Allow = "true"
auth.FullBind = "true"
auth.XMLNS = "http://www.google.com/talk/protocol/auth"
auth.Credential = credential
if err := xmlEncoder.Encode(auth); err != nil {
return fmt.Errorf("Failed to write SASL credentials: %s", err)
}
var success struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-sasl success"`
}
if err := xmlDecoder.Decode(&success); err != nil {
return fmt.Errorf("Failed to complete SASL handshake: %s", err)
}
return nil
}
示例13: MarshalXML
// MarshalXML is a weird and stunted version of xml.Marshal which is used by the
// REST-XML request types to get around a bug in encoding/xml which doesn't
// allow us to marshal pointers to zero values:
//
// https://github.com/golang/go/issues/5452
func MarshalXML(v interface{}, e *xml.Encoder, start xml.StartElement) error {
value := reflect.ValueOf(v)
t := value.Type()
switch value.Kind() {
case reflect.Ptr:
if !value.IsNil() {
return MarshalXML(value.Elem().Interface(), e, start)
}
case reflect.Struct:
var rootInfo xmlFieldInfo
// detect xml.Name, if any
for i := 0; i < value.NumField(); i++ {
f := t.Field(i)
v := value.Field(i)
if f.Type == xmlName {
rootInfo = parseXMLTag(f.Tag.Get("xml"))
if rootInfo.name == "" {
// name not in tag, try value
name := v.Interface().(xml.Name)
rootInfo = xmlFieldInfo{
name: name.Local,
ns: name.Space,
}
}
}
}
for _, start := range rootInfo.start(t.Name()) {
if err := e.EncodeToken(start); err != nil {
return err
}
}
for i := 0; i < value.NumField(); i++ {
ft := value.Type().Field(i)
if ft.Type == xmlName {
continue
}
fv := value.Field(i)
fi := parseXMLTag(ft.Tag.Get("xml"))
if fi.name == "-" {
continue
}
if fi.omit {
switch fv.Kind() {
case reflect.Ptr:
if fv.IsNil() {
continue
}
case reflect.Slice, reflect.Map:
if fv.Len() == 0 {
continue
}
default:
if !fv.IsValid() {
continue
}
}
}
starts := fi.start(ft.Name)
for _, start := range starts[:len(starts)-1] {
if err := e.EncodeToken(start); err != nil {
return err
}
}
start := starts[len(starts)-1]
if err := e.EncodeElement(fv.Interface(), start); err != nil {
return err
}
for _, end := range fi.end(ft.Name)[1:] {
if err := e.EncodeToken(end); err != nil {
return err
}
}
}
for _, end := range rootInfo.end(t.Name()) {
if err := e.EncodeToken(end); err != nil {
return err
}
}
default:
return e.Encode(v)
}
return nil
}
示例14: MarshalXML
func (fw *Feature) MarshalXML(e *xml.Encoder, _ xml.StartElement) error {
return e.Encode(fw.featureElement)
}
示例15: MarshalXML
func (u *extern) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
switch x := u.x.(type) {
case *ir.Unit:
start.Name.Local = "unit"
u.attr(&start, "name", x.Name)
err = e.EncodeToken(start)
for _, c := range x.Const {
n := &extern{x: c}
e.Encode(n)
}
for _, v := range x.Variables {
n := &extern{x: v}
e.Encode(n)
}
if len(x.Infix) > 0 {
inf := xml.StartElement{}
inf.Name.Local = "infix"
u.attr(&inf, "num", len(x.Infix))
for i, v := range x.Infix {
u.attr(&inf, "arg"+strconv.Itoa(i), v.Name)
}
e.EncodeToken(inf)
e.EncodeToken(inf.End())
}
for _, v := range x.Pre {
pre := xml.StartElement{}
pre.Name.Local = "precondition"
e.EncodeToken(pre)
n := &extern{x: v}
e.Encode(n)
e.EncodeToken(pre.End())
}
for _, v := range x.Post {
post := xml.StartElement{}
post.Name.Local = "postcondition"
e.EncodeToken(post)
n := &extern{x: v}
e.Encode(n)
e.EncodeToken(post.End())
}
err = e.EncodeToken(start.End())
case ir.ForeignType:
start.Name.Local = "definition"
u.attr(&start, "name", x.Name())
err = e.EncodeToken(start)
for _, v := range x.Variables() {
n := &extern{x: v, shallow: true}
e.Encode(n)
}
for _, v := range x.Imports() {
imp := xml.StartElement{}
imp.Name.Local = "import"
u.attr(&imp, "name", v)
e.EncodeToken(imp)
e.EncodeToken(imp.End())
}
if len(x.Infix()) > 0 {
inf := xml.StartElement{}
inf.Name.Local = "infix"
u.attr(&inf, "num", len(x.Infix()))
for i, v := range x.Infix() {
u.attr(&inf, "arg"+strconv.Itoa(i), v.Name)
}
e.EncodeToken(inf)
e.EncodeToken(inf.End())
}
err = e.EncodeToken(start.End())
case *ir.Variable:
switch x.Modifier {
case mods.IN:
start.Name.Local = "in"
case mods.OUT:
start.Name.Local = "out"
case mods.REG:
start.Name.Local = "reg"
default:
start.Name.Local = "var"
}
u.attr(&start, "name", x.Name)
u.attr(&start, "builtin", x.Type.Basic)
if x.Type.Basic {
u.attr(&start, "type", x.Type.Builtin.Code.String())
} else {
u.attr(&start, "type", x.Type.Foreign.Name())
}
e.EncodeToken(start)
if x.Type.Basic && !u.shallow {
if r := x.Unit.Rules[x.Name]; r != nil {
n := &extern{x: r}
e.Encode(n)
}
} else if !u.shallow {
if rr := x.Unit.ForeignRules[x.Name]; rr != nil {
for k, v := range rr {
rs := xml.StartElement{}
rs.Name.Local = "foreign"
u.attr(&rs, "id", k)
e.EncodeToken(rs)
n := &extern{x: v}
e.Encode(n)
//.........這裏部分代碼省略.........