Xml in CLIPS

Our example demonstrates XML processing in Java using the JAXB (Java Architecture for XML Binding) API. This API 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;
    
    @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);
    }
}

In this class, we use JAXB annotations to specify how the object should be represented in XML. The @XmlRootElement annotation defines the name of the root XML element, while @XmlAttribute and @XmlElement annotations specify which fields should be attributes or elements in the XML.

Now, let’s create a Nesting class to demonstrate nested XML structures:

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

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

Here’s our main class demonstrating XML operations:

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(27, "Coffee", Arrays.asList("Ethiopia", "Brazil"));

        JAXBContext context = JAXBContext.newInstance(Plant.class);
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        StringWriter writer = new StringWriter();
        marshaller.marshal(coffee, writer);
        String xml = writer.toString();
        System.out.println(xml);

        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xml);

        Unmarshaller unmarshaller = context.createUnmarshaller();
        Plant p = (Plant) unmarshaller.unmarshal(new StringReader(xml));
        System.out.println(p);

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

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

        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 example demonstrates:

  1. Creating Java objects representing XML structures.
  2. Marshalling Java objects to XML using JAXB.
  3. Unmarshalling XML back into Java objects.
  4. Handling nested XML structures.

To run this program, compile it and execute:

$ javac XMLExample.java
$ java XMLExample

The output will show the XML representations of our Java objects and demonstrate the marshalling and unmarshalling processes.

Note that unlike the original example which uses a low-level XML API, this Java version uses JAXB, which provides a higher-level, more object-oriented approach to XML processing. This is more idiomatic in Java applications.