Back to

Package xml

Latest Go to latest

The latest major version is v2.

Published: Feb 8, 2019 | License: Apache-2.0 | Module:


Package xml defines types that are specific to handling web service requests and responses as XML. Components implementing this type will be created when you enable the XmlWs facility. For more information on XML web services in Granitic, see


HTTP request bodies sent to a Granitic application with the XmlWs facility enabled are unmarshalled from XML into a Go struct using Go's built-in XML unmarshalling techniques. See

Templated or marshalling mode

The data generated by your web service endpoints can be serialised to XML in two ways by setting either:

  "XmlWs": {
    "ResponseMode": "MARSHAL"


  "XmlWs": {
    "ResponseMode": "TEMPLATE"

in your application configuration file.

In MARSHAL mode the data and errors in your endpoint's WsResponse objects are serialised using Go's built-in XML marshalling techniques. See In TEMPLATE mode each endpoint is associated with the name of a template file which is populated with the data and errors in your response. See for more details.

Response wrapping

In MARSHAL mode, any data serialised to XML will first be wrapped with a containing data structure by an instance of GraniticXmlResponseWrapper. This means that all responses share a common top level structure for finding the body of the response or errors if they exist. For more information on this behaviour (and how to override it) see:

Error formatting

Any service errors found in a response are formatted by an instance of GraniticXmlErrorFormatter before being serialised to XML. For more information on this behaviour (and how to override it) see:


type GraniticXmlError

type GraniticXmlError struct {
	XMLName  xml.Name
	Error    string `xml:",chardata"`
	Field    string `xml:"field,attr,omitempty"`
	Code     string `xml:"code,attr,omitempty"`
	Category string `xml:"category,attr,omitempty"`

Default XML representation of a service error. See ws.CategorisedError

type GraniticXmlErrorFormatter

type GraniticXmlErrorFormatter struct{}

Converts service errors into a data structure for consistent serialisation to XML.

func (*GraniticXmlErrorFormatter) FormatErrors

func (ef *GraniticXmlErrorFormatter) FormatErrors(errors *ws.ServiceErrors) interface{}

FormatErrors converts all of the errors present in the supplied objects into a structure suitable for serialisation.

type GraniticXmlResponseWrapper

type GraniticXmlResponseWrapper struct {

Component for wrapping response data in a common strcuture before it is serialised.

func (*GraniticXmlResponseWrapper) WrapResponse

func (rw *GraniticXmlResponseWrapper) WrapResponse(body interface{}, errors interface{}) interface{}

WrapResponse wraps the supplied data and errors with an XmlWrapper

type GraniticXmlWrapper

type GraniticXmlWrapper struct {
	XMLName xml.Name
	Errors  interface{}
	Body    interface{} `xml:"body"`

Wrapper for web service data and errors giving a consistent structure across all XML endpoints.

type StandardXmlUnmarshaller

type StandardXmlUnmarshaller struct {

Component wrapper over Go's xml.Unmarshal method

func (*StandardXmlUnmarshaller) Unmarshall

func (um *StandardXmlUnmarshaller) Unmarshall(ctx context.Context, req *http.Request, wsReq *ws.WsRequest) error

Unmarshall decodes XML into a Go struct using Go's builtin xml.Unmarshal method.

type TemplatedXmlResponseWriter

type TemplatedXmlResponseWriter struct {
	// Injected by the framework to allow this component to write log messages
	FrameworkLogger logging.Logger

	// A component able to calculate the correct HTTP status code to set for a response.
	StatusDeterminer ws.HttpStatusCodeDeterminer

	// Component able to generate errors if a problem is encountered during marshalling.
	FrameworkErrors *ws.FrameworkErrorGenerator

	// The common and static set of headers that should be written to all responses.
	DefaultHeaders map[string]string

	// The path (absolute or relative to application working directory) where unpopulated template files can be found.
	TemplateDir string

	// A map from an HTTP status code (e.g. '404') to the name of the template to be used to render that type of response.
	StatusTemplates map[string]string

	// Component able to dynamically generate additional headers to be written to the response.
	HeaderBuilder ws.WsCommonResponseHeaderBuilder

	//The name of a template to be used if the response has an abnormal (5xx) outcome.
	AbnormalTemplate string

	//The name of the default template to use if the response an error (400 or 409) outcome.
	ErrorTemplate string
	// contains filtered or unexported fields

Serialises the body of a ws.WsResponse to XML using Go templates. See

func (*TemplatedXmlResponseWriter) StartComponent

func (rw *TemplatedXmlResponseWriter) StartComponent() error

Called by the IoC container. Verifies that at minimum the AbnormalTemplate and TemplateDir fields are set. Parses all templates found in the TemplateDir

func (*TemplatedXmlResponseWriter) Write

func (rw *TemplatedXmlResponseWriter) Write(ctx context.Context, state *ws.WsProcessState, outcome ws.WsOutcome) error

See WsResponseWriter.Write

func (*TemplatedXmlResponseWriter) WriteAbnormalStatus

func (rw *TemplatedXmlResponseWriter) WriteAbnormalStatus(ctx context.Context, state *ws.WsProcessState) error

See AbnormalStatusWriter.WriteAbnormalStatus

type XmlErrors

type XmlErrors struct {
	XMLName xml.Name
	Errors  interface{}

Wrapper to create an errors element in generated XML

type XmlMarshalingWriter

type XmlMarshalingWriter struct {
	// Format generated XML in a human readable form.
	PrettyPrint bool

	// The characters (generally tabs or spaces) to indent child elements in pretty-printed XML.
	IndentString string

	// A prefix for each line of generated XML.
	PrefixString string

Component wrapper over Go's xml.Marshalxx functions. Serialises a struct to XML and writes it to the HTTP response output stream.

func (*XmlMarshalingWriter) MarshalAndWrite

func (mw *XmlMarshalingWriter) MarshalAndWrite(data interface{}, w http.ResponseWriter) error

MarshalAndWrite serialises the supplied interface to XML and writes it to the HTTP response output stream.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier