Alb*_*cht 5 java xml tree jaxb unmarshalling
我有 XML 输入,它本质上是一个树结构。目标是(取消)将代码编组到 Java 类中。
<config>
<key-value-pair>
<key>Key1</key>
<value>Value1</value>
</key-value-pair>
<key-value-pair>
<key>Key2</key>
<value>
<key-value-pair>
<key>Subkey2</key>
<value>Value999</value>
</key-value-pair>
</value>
</key-value-pair>
</config>
Run Code Online (Sandbox Code Playgroud)
XML 包含典型的键/值对。和每一个值可以包含另一个键/值对,或列表键/值对或只是一个单一的字符串值。
<config>
<key-value-pair>
<key>Key1</key>
<value>Value1</value>
</key-value-pair>
<key-value-pair>
<key>Key2</key>
<value>
<key-value-pair>
<key>Subkey2</key>
<value>Value999</value>
</key-value-pair>
</value>
</key-value-pair>
</config>
Run Code Online (Sandbox Code Playgroud)
然后我只有另一个包装类
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
static class KeyValuePair {
@XmlElement(name="key")
private String key;
@XmlElement(name="value")
private String value; // here I don't know how to reflect
// the choice of String or another
// list of KeyValuePair objects
@XmlElement(name="value")
private List<KeyValuePair> valuePairs;
// getters/setters here
}
Run Code Online (Sandbox Code Playgroud)
这是我试图用于(取消)编组的逻辑。
@XmlRootElement(name="config")
@XmlAccessorType(XmlAccessType.FIELD)
static class Structure {
@XmlElement(name="key-value-pair")
private List<KeyValuePair> keyValuePair;
// getters/setters
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所得到的。它不像我所拥有的那样工作,但我希望我自己清楚最终目标应该是什么样子。
我想把开头的 XML 放在 Structure 类的实例中。
您可能已经想到了这一点...如果您首先在模式级别对其进行建模,如下所示;
<?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>
Run Code Online (Sandbox Code Playgroud)
然后使用 XJC Ant Task或以编程方式调用它或以其他方式生成最终得到的 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;
/**
* <p>Java class for key-value-pair complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType name="key-value-pair">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="key" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="value">
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <choice>
* <element name="key-value-pair" type="{}key-value-pair" minOccurs="0"/>
* </choice>
* </restriction>
* </complexContent>
* </complexType>
* </element>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@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;
/**
* Gets the value of the key property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getKey() {
return key;
}
/**
* Sets the value of the key property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setKey(String value) {
this.key = value;
}
/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link KeyValuePair.Value }
*
*/
public KeyValuePair.Value getValue() {
return value;
}
/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link KeyValuePair.Value }
*
*/
public void setValue(KeyValuePair.Value value) {
this.value = value;
}
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <choice>
* <element name="key-value-pair" type="{}key-value-pair" minOccurs="0"/>
* </choice>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@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;
/**
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link String }
* {@link JAXBElement }{@code <}{@link KeyValuePair }{@code >}
*
*
*/
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
}
}
Run Code Online (Sandbox Code Playgroud)
再加上一个 ObjectFactory 和一个 Config 类(本质上是您的 Structure 类)。ObjectFactory 为我们提供了一些“糖”来帮助我们完成可怕的 JAXBElement 舞蹈。
正如 Andreas 所说,仍然允许 String 和嵌套 KeyValuePair。然后,您必须使用验证器包装 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)");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要创建配置,您必须执行 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;
}
Run Code Online (Sandbox Code Playgroud)
嗯,这是一个开始...