Skip to content

encoding/xml: allow avoiding infinite recursion in EncodeElement #6474

Closed
@lukescott

Description

@lukescott
When using MarshalXML if you want to modify just the surrounding tag it's difficult to
do so. Calling EncodeElement results in a recursive error.

The only work around, other than manually marshaling each field, is to typedef the
struct to an alternate type:


type _TransactionRequest TransactionRequest;

func (tr TransactionRequest) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    return e.EncodeElement(_TransactionRequest(tr), xml.StartElement{
            Name: xml.Name{"", "createTransactionRequest"},
            Attr: []xml.Attr{
                    xml.Attr{xml.Name{"", "xmlns:xsi"}, "http://www.w3.org/2001/XMLSchema-instance";},
                    xml.Attr{xml.Name{"", "xmlns:xsd"}, "http://www.w3.org/2001/XMLSchema";},
                    xml.Attr{xml.Name{"", "xmlns"}, "AnetApi/xml/v1/schema/AnetApiSchema.xsd"},
                },
        })
}


There needs to be a way to Marshal the contents of the struct without the surrounding
tag, perhaps "EncodeBody":


func (tr TransactionRequest) MarshalXML(e *xml.Encoder, start xml.StartElement) (err
error) {
    start = xml.StartElement{
            Name: xml.Name{"", "createTransactionRequest"},
            Attr: []xml.Attr{
                xml.Attr{xml.Name{"", "xmlns:xsi"}, "http://www.w3.org/2001/XMLSchema-instance";},
                xml.Attr{xml.Name{"", "xmlns:xsd"}, "http://www.w3.org/2001/XMLSchema";},
                xml.Attr{xml.Name{"", "xmlns"}, "AnetApi/xml/v1/schema/AnetApiSchema.xsd"},
            },
        }
    if err = e.EncodeToken(start); err != nil {
        return
    }
    if err = e.EncodeBody(tr); err != nil {
        return
    }
    if err = e.EncodeToken(EndElement{start.Name}); err != nil {
        return
    }
}


Which compiler are you using (5g, 6g, 8g, gccgo)?

gc default

Which operating system are you using?

OS X 10.8.5

Which version are you using?  (run 'go version')

go version devel +f4d1cb8d9a91 Thu Sep 19 22:34:33 2013 +1000 darwin/amd64

Please provide any additional information below.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions