XML
Content-Type: "text/xml"
Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.
The Context.XML(v, ...opts) is the method which sends XML responses to the client. It accepts the value and optional settings for rendering. The XML options structure looks like this:
1
type XML struct {
2
Indent string
3
Prefix string
4
}
Copied!
If Indent field is empty and the application runs without optimizations, the Indent field will be automatically set to 2 spaces.
Render a Go struct as XML response:
1
type ExampleXML struct {
2
XMLName xml.Name `xml:"example"`
3
One string `xml:"one,attr"`
4
Two string `xml:"two,attr"`
5
}
6
7
func handler(ctx iris.Context) {
8
response := ExampleXML{One: "hello", Two: "xml"}
9
ctx.XML(response)
10
}
Copied!
Result
1
<example one="hello" two="xml"/>
Copied!
You can NOT provide a raw map or iris.Map on context.XML because the value should complete the xml.Marshaler interface. Iris has the iris.XMLMap which converts a map (or iris.Map) to xml Marshaler. That helper function accepts the root level name and a map. Example:
1
func handler(ctx iris.Context) {
2
kv := iris.Map{"key": "value"}
3
response := iris.XMLMap("keys", kv)
4
ctx.XML(response, iris.XML{Indent: " "})
5
}
Copied!
Result
1
<keys>
2
<key>value</key>
3
</keys>
Copied!
References:
Last modified 1yr ago
Export as PDF
Copy link