// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;

import generated.AObjectElementTYPE;
import generated.PayloadinformationTYPE;

public class Test {
    @XmlType(name="anyType", namespace="http://www.w3.org/2001/XMLSchema")
    public static class Container {
        private List<JAXBElement<?>> tags;
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public Container(Object o, String tag) {
            this.tags = new ArrayList<>();
            tags.add(new JAXBElement(new QName(tag), o.getClass(), o));
        }
        @XmlAnyElement
        public List<JAXBElement<?>> getProperties() {
            return tags;
        }
        public void setProperties(List<JAXBElement<?>> tags) {
            throw new RuntimeException("Not implemented");
        }
    }
    public static void main(String[] args) throws JAXBException {
        AObjectElementTYPE aoet = new AObjectElementTYPE();
        aoet.setChildElementOne(123);
        aoet.setChildElementTwo("ABC");
        PayloadinformationTYPE plit = new PayloadinformationTYPE();
        plit.setPayload(new Container(aoet, "AObjectElement"));
        JAXBContext ctx = JAXBContext.newInstance(PayloadinformationTYPE.class, AObjectElementTYPE.class, Container.class);
        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<PayloadinformationTYPE> je = new JAXBElement<>(new QName("", "xsdone:PayloadinformationTYPE"), PayloadinformationTYPE.class, plit);
        StringWriter sw = new StringWriter();
        m.marshal(je, sw);
        System.out.print(sw.toString());
    }
}
