Xml

Go offers built-in support for XML and XML-like formats with the encoding/xml package.

Here’s how we can achieve similar functionality in Java using JAXB (Java Architecture for XML Binding).

import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;

@XmlRootElement(name = "plant")
class Plant {
    private int id;
    private String name;
    private List<String> origin;

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public List<String> getOrigin() {
        return origin;
    }

    public void setOrigin(List<String> origin) {
        this.origin = origin;
    }

    @Override
    public String toString() {
        return String.format("Plant id=%d, name=%s, origin=%s", id, name, origin);
    }
}

public class Main {
    public static void main(String[] args) throws JAXBException {
        Plant coffee = new Plant();
        coffee.setId(27);
        coffee.setName("Coffee");
        coffee.setOrigin(Arrays.asList("Ethiopia", "Brazil"));

        // Emit XML representing our plant
        JAXBContext context = JAXBContext.newInstance(Plant.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        StringWriter writer = new StringWriter();
        marshaller.marshal(coffee, writer);
        String xmlOutput = writer.toString();
        System.out.println(xmlOutput);

        // Add a generic XML header
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xmlOutput);

        // Use Unmarshaller to parse a stream of bytes with XML into a data structure
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Plant unmarshalledPlant = (Plant) unmarshaller.unmarshal(new StringReader(xmlOutput));
        System.out.println(unmarshalledPlant);

        Plant tomato = new Plant();
        tomato.setId(81);
        tomato.setName("Tomato");
        tomato.setOrigin(Arrays.asList("Mexico", "California"));

        // If you need to nest XML elements, you might create a container class
        @XmlRootElement(name = "nesting")
        class Nesting {
            private List<Plant> plants;

            @XmlElement(name = "plant")
            public List<Plant> getPlants() {
                return plants;
            }

            public void setPlants(List<Plant> plants) {
                this.plants = plants;
            }
        }

        Nesting nesting = new Nesting();
        nesting.setPlants(Arrays.asList(coffee, tomato));

        JAXBContext nestingContext = JAXBContext.newInstance(Nesting.class);
        Marshaller nestingMarshaller = nestingContext.createMarshaller();
        nestingMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        StringWriter nestingWriter = new StringWriter();
        nestingMarshaller.marshal(nesting, nestingWriter);
        String nestingOutput = nestingWriter.toString();
        System.out.println(nestingOutput);
    }
}

Compile and run the program to see the XML output:

$ javac -cp jakarta.xml.bind-api-3.0.2.jar:jakarta.activation-api-1.2.2.jar Main.java
$ java -cp .:jakarta.xml.bind-api-3.0.2.jar:jakarta.activation-api-1.2.2.jar Main

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