Xml in Miranda

Our program demonstrates XML processing in Java. We’ll use the built-in javax.xml.bind package for XML operations.

import javax.xml.bind.annotation.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.Arrays;

@XmlRootElement(name = "plant")
public class Plant {
    @XmlAttribute
    private int id;

    @XmlElement
    private String name;

    @XmlElement(name = "origin")
    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);
    }
}

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

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

        // Emit XML representing our plant
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(coffee, writer);
        String xmlOutput = writer.toString();
        System.out.println(xmlOutput);

        // To add a generic XML header to the output, we can prepend it
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlOutput);

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

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

        // For nested XML, we need to create a wrapper class
        @XmlRootElement(name = "nesting")
        class Nesting {
            @XmlElementWrapper(name = "parent")
            @XmlElementWrapper(name = "child")
            @XmlElement(name = "plant")
            List<Plant> plants;
        }

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

        // Create a new context for the Nesting class
        context = JAXBContext.newInstance(Nesting.class);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new StringWriter();
        marshaller.marshal(nesting, writer);
        System.out.println(writer.toString());
    }
}

This Java code demonstrates XML processing using the JAXB (Java Architecture for XML Binding) API. Here’s a breakdown of what’s happening:

  1. We define a Plant class annotated with JAXB annotations to control XML serialization.

  2. In the main method, we create a Plant object and use JAXBContext and Marshaller to convert it to XML.

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

  4. We use Unmarshaller to parse the XML back into a Java object.

  5. To demonstrate nested XML, we create an inner Nesting class and show how to marshal it to XML.

The output will be similar to the Go example, showing the XML representation of the Plant and Nesting objects.

To run this program, save it as XMLExample.java, compile it with javac XMLExample.java, and run it with java XMLExample.

Note that JAXB is part of Java SE up to Java 8. For Java 9 and later, you might need to add JAXB as a dependency to your project.