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:

// 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.

Last updated