# 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:

```go
type XML struct {
	Indent string
	Prefix string
}
```

> 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:

```go
type ExampleXML struct {
	XMLName xml.Name `xml:"example"`
	One     string   `xml:"one,attr"`
	Two     string   `xml:"two,attr"`
}

func handler(ctx iris.Context) {
    response := ExampleXML{One: "hello", Two: "xml"}
    ctx.XML(response)
}
```

**Result**

```xml
<example one="hello" two="xml"/>
```

You can **NOT** provide a raw `map` or `iris.Map` on `context.XML` because the value should complete the [xml.Marshaler](https://godoc.org/encoding/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:

```go
func handler(ctx iris.Context) {
    kv := iris.Map{"key": "value"}
    response := iris.XMLMap("keys", kv)
    ctx.XML(response, iris.XML{Indent: "    "})
}
```

**Result**

```xml
<keys>
    <key>value</key>
</keys>
```

References:

* [Specification](https://www.w3.org/TR/REC-xml/)
* [SOAP vs REST vs JSON - a 2020 comparison](https://raygun.com/blog/soap-vs-rest-vs-json/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://iris-go.gitbook.io/iris/responses/xml.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
