Xml in Logo

Here’s the translation of the XML example from Go to Java:

Our program demonstrates XML processing in Java using the JAXB (Java Architecture for XML Binding) API. Here’s the full source code:

import jakarta.xml.bind.annotation.*;
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.List;

// Plant will be mapped to XML. We use JAXB annotations to specify
// how the class should be represented in XML.
@XmlRootElement(name = "plant")
@XmlAccessorType(XmlAccessType.FIELD)
class Plant {
    @XmlAttribute
    private int id;
    
    @XmlElement
    private String name;
    
    @XmlElement
    private List<String> origin;

    // Constructors, getters, and setters omitted for brevity

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

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

    // Constructor, getters, and setters omitted for brevity
}

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

        // Create a JAXBContext for the Plant class
        JAXBContext context = JAXBContext.newInstance(Plant.class);

        // Create a Marshaller to convert the Plant object to XML
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Convert the Plant object to XML and print it
        StringWriter writer = new StringWriter();
        marshaller.marshal(coffee, writer);
        System.out.println(writer.toString());

        // To add a generic XML header to the output, we need to set it explicitly
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
        writer = new StringWriter();
        marshaller.marshal(coffee, writer);
        System.out.println(writer.toString());

        // Use Unmarshaller to parse the XML string back into a Plant object
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Plant p = (Plant) unmarshaller.unmarshal(new StringReader(writer.toString()));
        System.out.println(p);

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

        // Create a Nesting object to demonstrate nested XML structures
        Nesting nesting = new Nesting();
        nesting.setPlants(List.of(coffee, tomato));

        // Create a JAXBContext for the Nesting class
        context = JAXBContext.newInstance(Nesting.class);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Convert the Nesting object to XML and print it
        writer = new StringWriter();
        marshaller.marshal(nesting, writer);
        System.out.println(writer.toString());
    }
}

This Java program demonstrates XML processing using JAXB. Here’s a breakdown of what it does:

  1. We define a Plant class with JAXB annotations to specify how it should be represented in XML.

  2. We also define a Nesting class to demonstrate nested XML structures.

  3. In the main method, we create a Plant object representing coffee.

  4. We use a JAXBContext and Marshaller to convert the Plant object to XML and print it.

  5. We demonstrate how to add an XML header to the output.

  6. We use an Unmarshaller to parse the XML string back into a Plant object.

  7. We create another Plant object representing a tomato.

  8. We create a Nesting object containing both plant objects to demonstrate nested XML structures.

  9. Finally, we convert the Nesting object to XML and print it.

To run this program, you would compile it and then execute it:

$ javac XMLExample.java
$ java XMLExample

This will output the XML representations of the Plant and Nesting objects.

Note that this example uses Jakarta XML Binding (formerly known as JAXB). You might need to add the appropriate dependencies to your project to use these classes.