Xml in Squirrel
Here’s the translation of the XML example from Go to Java, formatted in Markdown suitable for Hugo:
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.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
@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 static void main(String[] args) throws Exception {
Plant coffee = new Plant();
coffee.id = 27;
coffee.name = "Coffee";
coffee.origin = Arrays.asList("Ethiopia", "Brazil");
// Create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Plant.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Marshal the plant object to XML
StringWriter sw = new StringWriter();
m.marshal(coffee, sw);
String xmlString = sw.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 um = context.createUnmarshaller();
Plant p = (Plant) um.unmarshal(new StringReader(xmlString));
System.out.println(p);
Plant tomato = new Plant();
tomato.id = 81;
tomato.name = "Tomato";
tomato.origin = Arrays.asList("Mexico", "California");
@XmlRootElement(name = "nesting")
class Nesting {
@XmlElement(name = "parent")
private Parent parent;
class Parent {
@XmlElement(name = "child")
private Child child;
class Child {
@XmlElement(name = "plant")
private List<Plant> plants;
}
}
}
Nesting nesting = new Nesting();
nesting.parent = nesting.new Parent();
nesting.parent.child = nesting.parent.new Child();
nesting.parent.child.plants = Arrays.asList(coffee, tomato);
JAXBContext nestingContext = JAXBContext.newInstance(Nesting.class);
Marshaller nestingMarshaller = nestingContext.createMarshaller();
nestingMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter nestingSw = new StringWriter();
nestingMarshaller.marshal(nesting, nestingSw);
System.out.println(nestingSw.toString());
}
}
This Java code demonstrates XML processing using the JAXB (Java Architecture for XML Binding) API. We define a Plant
class with XML annotations to control how it’s serialized to and deserialized from XML.
The main
method showcases various XML operations:
- We create a
Plant
object and marshal it to XML using JAXB. - We print the XML, both with and without the XML declaration.
- We unmarshal the XML back into a
Plant
object. - We demonstrate nested XML structures using inner classes.
To run this program, save it as XMLExample.java
, compile it, and run it:
$ javac XMLExample.java
$ java XMLExample
This will output the XML representations of our data structures, demonstrating both simple and nested XML processing in Java.
Note that JAXB is part of Java SE in versions 8 and earlier. For Java 9 and later, you might need to add JAXB as a dependency to your project.