Xml in Fortress
Here’s the translation of the XML example from Go to Java, formatted in Markdown suitable for Hugo:
Our program demonstrates working with XML in Java. We’ll use the JAXB (Java Architecture for XML Binding) API, which is part of the Java SE platform.
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;
@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);
}
}
In this class, we use JAXB annotations to specify how the object should be mapped to XML. The @XmlRootElement
annotation specifies the name of the root XML element, and @XmlAttribute
and @XmlElement
annotations mark fields as XML attributes or elements respectively.
Now, let’s create our main class to demonstrate XML marshalling and unmarshalling:
import javax.xml.bind.*;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
public class XMLExample {
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 marshaller
JAXBContext context = JAXBContext.newInstance(Plant.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Marshal the coffee 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 p = (Plant) unmarshaller.unmarshal(new StringReader(xmlString));
System.out.println(p);
// 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")
@XmlElement(name = "child")
private List<Plant> plants;
// Getter and setter omitted for brevity
}
This program demonstrates XML marshalling (converting objects to XML) and unmarshalling (parsing XML back into objects). We also show how to create nested XML structures.
To run this program, you’ll need to compile and execute it:
$ javac XMLExample.java
$ java XMLExample
The output will show the XML representations of our Plant and Nesting objects, demonstrating both simple and nested XML structures.
Note that JAXB is included in Java SE 8 and earlier versions. For Java SE 9 and later, you might need to add JAXB as a dependency to your project.
This example showcases basic XML handling in Java, including object-to-XML mapping, formatting output, and parsing XML back into objects.