> For the complete documentation index, see [llms.txt](https://ineelhere.gitbook.io/code-with-go/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ineelhere.gitbook.io/code-with-go/53-xml.md).

# XML

This Go code demonstrates the use of the `encoding/xml` package for encoding and decoding XML data. Let's go through the code with inline comments and explanations:

```go
// Importing necessary packages.
import (
	"encoding/xml"
	"fmt"
)

// Struct definition for the Plant with XML tags.
type Plant struct {
	XMLName xml.Name `xml:"plant"`
	Id      int      `xml:"id,attr"`
	Name    string   `xml:"name"`
	Origin  []string `xml:"origin"`
}

// String method for Plant to customize its string representation.
func (p Plant) String() string {
	return fmt.Sprintf("Plant id=%v, name=%v, origin=%v",
		p.Id, p.Name, p.Origin)
}

// The main function, where the execution of the program begins.
func main() {
	// Creating a Plant instance (coffee) and encoding it to XML.
	coffee := &Plant{Id: 27, Name: "Coffee"}
	coffee.Origin = []string{"Ethiopia", "Brazil"}

	// Encoding the Plant to XML with indentation.
	out, _ := xml.MarshalIndent(coffee, " ", "  ")
	fmt.Println(string(out))

	// Printing XML with header.
	fmt.Println(xml.Header + string(out))

	// Decoding XML back into a Plant instance.
	var p Plant
	if err := xml.Unmarshal(out, &p); err != nil {
		panic(err)
	}
	fmt.Println(p)

	// Creating another Plant instance (tomato) and adding it to a nested structure.
	tomato := &Plant{Id: 81, Name: "Tomato"}
	tomato.Origin = []string{"Mexico", "California"}

	// Defining a struct with nested Plants and encoding it to XML.
	type Nesting struct {
		XMLName xml.Name `xml:"nesting"`
		Plants  []*Plant `xml:"parent>child>plant"`
	}

	nesting := &Nesting{}
	nesting.Plants = []*Plant{coffee, tomato}

	// Encoding the nested structure to XML with indentation.
	out, _ = xml.MarshalIndent(nesting, " ", "  ")
	fmt.Println(string(out))
}
```

#### Output

```
 <plant id="27">
   <name>Coffee</name>
   <origin>Ethiopia</origin>
   <origin>Brazil</origin>
 </plant>
<?xml version="1.0" encoding="UTF-8"?>
 <plant id="27">
   <name>Coffee</name>
   <origin>Ethiopia</origin>
   <origin>Brazil</origin>
 </plant>
Plant id=27, name=Coffee, origin=[Ethiopia Brazil]
 <nesting>
   <parent>
     <child>
       <plant id="27">
         <name>Coffee</name>
         <origin>Ethiopia</origin>
         <origin>Brazil</origin>
       </plant>
       <plant id="81">
         <name>Tomato</name>
         <origin>Mexico</origin>
         <origin>California</origin>
       </plant>
     </child>
   </parent>
 </nesting>
```

Explanation:

1. **Struct Definition with XML Tags:**
   * The `Plant` struct has fields tagged with `xml` tags, specifying how the fields should be represented in XML.
2. **String Method:**
   * The `String` method is defined for the `Plant` struct to customize its string representation.
3. **Encoding and Decoding XML:**
   * A `Plant` instance (`coffee`) is encoded to XML using `xml.MarshalIndent`.
   * The XML is printed with and without the XML header.
   * The encoded XML is then decoded back into a `Plant` instance (`p`).
4. **Nested XML Structure:**
   * Another `Plant` instance (`tomato`) is created.
   * A struct (`Nesting`) is defined with a nested array of `Plant` instances.
   * The nested structure is encoded to XML with indentation.

This code demonstrates the basic usage of the `encoding/xml` package in Go for encoding and decoding XML data.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/53-xml.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
