Hallo TheIntruder,
TheIntruder schrieb:
Wie loese ich das?
das geht scheinbar wirklich nur so:
@XmlElement(defaultValue = "false")
protected Boolean help1;
@XmlElement(defaultValue = "false")
protected Boolean help2 = false;
@XmlElement(defaultValue = "false")
protected Boolean help3;
Falls das Element nicht im XML vorhanden ist wird dieser Wert gesetzt. Anderenfalls der default Wert gesetzt (Element leer) oder eben der Wert, der innerhalb des Elements steht.
Getestet habe ich es unter Oracle Java 8 und diesem Codefragment.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 | import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class Main
{
public static void main(String... args) throws JAXBException
{
JAXBContext jaxbContext = JAXBContext.newInstance(XmlDocument.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
XmlDocument unmarshal = (XmlDocument)unmarshaller.unmarshal(new StringReader(Main.testEmptyNode()));
System.out.println(unmarshal);
unmarshal = (XmlDocument)unmarshaller.unmarshal(new StringReader(Main.testMissingNode()));
System.out.println(unmarshal);
unmarshal = (XmlDocument)unmarshaller.unmarshal(new StringReader(Main.testNodeValue()));
System.out.println(unmarshal);
}
public static String testEmptyNode()
{
return "<xmlDocument><help1>false</help1><help2/><help3>true</help3></xmlDocument>";
}
public static String testMissingNode()
{
return "<xmlDocument><help1>false</help1><help3>true</help3></xmlDocument>";
}
public static String testNodeValue()
{
return "<xmlDocument><help1>false</help1><help2>true</help2><help3>true</help3></xmlDocument>";
}
@XmlRootElement
public static class XmlDocument
{
@XmlElement(defaultValue = "false")
protected Boolean help1;
@XmlElement(defaultValue = "false")
protected Boolean help2 = false;
@XmlElement(defaultValue = "false")
protected Boolean help3;
@Override
public String toString()
{
return "XmlDocument{" + "help1=" + help1 +
", help2=" + help2 +
", help3=" + help3 +
'}';
}
}
}
|
Ich hoffe das hilft weiter.
Gruß Stumml