import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import java.io.*;
import java.util.*;
@XmlRootElement(name="myroot")
@XmlAccessorType(XmlAccessType.NONE)
class SO19509130d {
@XmlAttribute
public String getId
() { return id
; } public void setId
(String id
) { this.
id = id
; }
@XmlElement(name="property")
public Collection<XmlProperty> getProperties() {
return new XmlProperties(props);
}
@XmlType(name="property") static class XmlProperty {
@XmlAttribute
public String key
; @XmlValue
public String value
; }
static class XmlProperties extends AbstractCollection<XmlProperty> {
public XmlProperties
(Properties props
) { this.
props = props
; } public int size() { return props.size(); }
public Iterator<XmlProperty> iterator() {
return new XmlPropertyIterator(props.entrySet().iterator());
}
public boolean add(XmlProperty xml) {
return !xml.value.equals(props.setProperty(xml.key, xml.value));
}
}
static class XmlPropertyIterator implements Iterator<XmlProperty> {
private final Iterator
<Map.
Entry<Object, Object
>> base
; public XmlPropertyIterator
(Iterator
<Map.
Entry<Object, Object
>> base
) { this.base = base;
}
public boolean hasNext() { return base.hasNext(); }
public void remove() { base.remove(); }
public XmlProperty next() {
Map.
Entry<?,
?> entry
= base.
next(); XmlProperty xml = new XmlProperty();
xml.key = entry.getKey().toString();
xml.value = entry.getValue().toString();
return xml;
}
}
SO19509130d obj = new SO19509130d();
obj.id = "theID";
obj.props.setProperty("key1", "val1");
obj.props.setProperty("key2", "val2");
JAXBContext ctx = JAXBContext.newInstance(SO19509130d.class);
Marshaller m = ctx.createMarshaller();
Unmarshaller u = ctx.createUnmarshaller();
m.marshal(obj, baos);
obj = (SO19509130d)u.unmarshal(bais);
}
}