, , ...
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="key-value-pair">
<xs:sequence>
<xs:element name="key" type="xs:string"/>
<xs:element name="value">
<xs:complexType mixed="true">
<xs:choice>
<xs:element name="key-value-pair" type="key-value-pair" minOccurs="0"/>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="config">
<xs:complexType>
<xs:sequence>
<xs:element name="key-value-pair" type="key-value-pair" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XJC Ant JAXB,
package uk.co.his.test.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "key-value-pair", propOrder = {
"key",
"value"
})
public class KeyValuePair {
@XmlElement(required = true)
protected String key;
@XmlElement(required = true)
protected KeyValuePair.Value value;
public String getKey() {
return key;
}
public void setKey(String value) {
this.key = value;
}
public KeyValuePair.Value getValue() {
return value;
}
public void setValue(KeyValuePair.Value value) {
this.value = value;
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
public static class Value {
@XmlElementRef(name = "key-value-pair", type = JAXBElement.class, required = false)
@XmlMixed
protected List<Serializable> content;
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
}
}
ObjectFactory Config ( Structure). ObjectFactory "", JAXBElement.
, String KeyValuePair (s). Config , , ; ,
package uk.co.his.test;
import java.io.Serializable;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import uk.co.his.test.model.Config;
import uk.co.his.test.model.KeyValuePair;
public class Validate {
public void validate(Config c) throws JAXBException
{
for(KeyValuePair kvp: c.getKeyValuePair())
{
validate(kvp);
}
}
public void validate(KeyValuePair kv) throws JAXBException
{
List<Serializable> mixed = kv.getValue().getContent();
boolean nonWhitespaceStringFound = false;
boolean kvpFound = false;
for(Serializable c: mixed)
{
if(c instanceof String)
{
String s = (String) c;
if(s.trim().length()>0) {
nonWhitespaceStringFound = true;
}
}
else
{
@SuppressWarnings("unchecked")
JAXBElement<KeyValuePair> t = (JAXBElement<KeyValuePair>) c;
KeyValuePair child = t.getValue();
kvpFound = true;
validate(child);
}
if(kvpFound && nonWhitespaceStringFound) {
throw new JAXBException("KeyValuePair "+kv.getKey()+" value element contained String data and nested KeyValuePair(s)");
}
}
}
}
, JAXBElement;
private static final File Test1Out = new File("files/test1.xml");
@Test
public void test1() throws JAXBException
{
ObjectFactory of = new ObjectFactory();
Config c = new Config();
c.getKeyValuePair().add(createKVPair(of, 2, 2, "a", "one"));
c.getKeyValuePair().add(createKVPair(of, 2, 1, "b", "two"));
c.getKeyValuePair().add(createKVPair(of, 0, 0, "c", "three"));
JAXBContext jbc = JAXBContext.newInstance("uk.co.his.test.model");
Marshaller m = jbc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(c, Test1Out);
Unmarshaller u = jbc.createUnmarshaller();
Config c2 = (Config) u.unmarshal(Test1Out);
Assert.assertTrue("Round trip produces different things", c2.getKeyValuePair().size() ==3);
}
private KeyValuePair createKVPair(ObjectFactory of, int depth, int length, String initialKey, String value) {
KeyValuePair kv = new KeyValuePair();
kv.setKey(initialKey);
Value v = new Value();
kv.setValue(v);
if(depth==0)
{
v.getContent().add(value);
}
else
{
int newdepth = --depth;
for(int i = 0; i < length; i++)
{
v.getContent().add(of.createKeyValuePairValueKeyValuePair(createKVPair(of, newdepth, length, initialKey+depth, value+i)));
}
}
return kv;
}
, ...