Xml in Minitab

Here’s the translation of the XML example from Go to Java, formatted in Markdown suitable for Hugo:

Our example demonstrates how to work with XML in Java using the JAXB (Java Architecture for XML Binding) library. JAXB provides a convenient way to marshal Java objects into XML and unmarshal XML back into Java objects.

First, let’s define our Plant class:

import javax.xml.bind.annotation.*;
import java.util.List;

@XmlRootElement(name = "plant")
@XmlAccessorType(XmlAccessType.FIELD)
public class Plant {
    @XmlAttribute
    private int id;
    
    @XmlElement
    private String name;
    
    @XmlElementWrapper(name = "origin")
    @XmlElement(name = "country")
    private List<String> origin;

    // Constructors, getters, and setters omitted for brevity

    @Override
    public String toString() {
        return "Plant id=" + id + ", name=" + name + ", origin=" + origin;
    }
}

Now, let’s create a Main class to demonstrate XML operations:

import javax.xml.bind.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;

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

        // Create a JAXB context and instantiate a marshaller
        JAXBContext context = JAXBContext.newInstance(Plant.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Marshal the plant object to XML
        StringWriter writer = new StringWriter();
        marshaller.marshal(coffee, writer);
        String xmlString = writer.toString();
        System.out.println(xmlString);

        // Add XML declaration
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        System.out.println(xmlString);

        // Unmarshal XML back to a Plant object
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Plant unmarshalledPlant = (Plant) unmarshaller.unmarshal(new StringReader(xmlString));
        System.out.println(unmarshalledPlant);

        // Create a nested structure
        Plant tomato = new Plant();
        tomato.setId(81);
        tomato.setName("Tomato");
        tomato.setOrigin(Arrays.asList("Mexico", "California"));

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

        // Marshal the nested structure
        context = JAXBContext.newInstance(Nesting.class);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(nesting, System.out);
    }
}

@XmlRootElement(name = "nesting")
@XmlAccessorType(XmlAccessType.FIELD)
class Nesting {
    @XmlElementWrapper(name = "parent")
    @XmlElementWrapper(name = "child")
    @XmlElement(name = "plant")
    private List<Plant> plants;

    // Getter and setter omitted for brevity
}

This Java code demonstrates XML marshalling and unmarshalling using JAXB. It creates a Plant object, converts it to XML, prints the XML, then unmarshals it back to a Plant object. It also shows how to create a nested XML structure.

To run this program, make sure you have the JAXB library in your classpath. For Java 9 and later, you might need to add the JAXB API as a dependency to your project.

The output will be similar to the following:

<plant id="27">
    <name>Coffee</name>
    <origin>
        <country>Ethiopia</country>
        <country>Brazil</country>
    </origin>
</plant>

<?xml version="1.0" encoding="UTF-8"?>
<plant id="27">
    <name>Coffee</name>
    <origin>
        <country>Ethiopia</country>
        <country>Brazil</country>
    </origin>
</plant>

Plant id=27, name=Coffee, origin=[Ethiopia, Brazil]

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<nesting>
    <parent>
        <child>
            <plant id="27">
                <name>Coffee</name>
                <origin>
                    <country>Ethiopia</country>
                    <country>Brazil</country>
                </origin>
            </plant>
            <plant id="81">
                <name>Tomato</name>
                <origin>
                    <country>Mexico</country>
                    <country>California</country>
                </origin>
            </plant>
        </child>
    </parent>
</nesting>

This example demonstrates how to work with XML in Java, including creating XML from objects, parsing XML into objects, and working with nested XML structures.