Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add List to JAXB example

  • Loading branch information...
commit ca47a2ec2b3df13077f637105e9ce92a16606291 1 parent 3c00e79
@bxt authored
View
11 unilectures/src/bxt/unilectures/fun/jaxb/Main.java
@@ -7,6 +7,9 @@
import javax.xml.bind.JAXB;
+import bxt.unilectures.fun.jaxb.list.TestImplementationA;
+import bxt.unilectures.fun.jaxb.list.TestImplementationB;
+
public class Main {
private static final String HALLO_WELT = "Hallo, Welt. ";
@@ -24,6 +27,14 @@ public static void main(String[] args) {
testRoot.setTestBean(testBean);
testRoot.setTestImmutable(new TestImmutable(2,3));
+ TestImplementationA testItemA = new TestImplementationA();
+ testItemA.setValue("A value. ");
+ testRoot.addTestItem(testItemA);
+
+ TestImplementationB testItemB = new TestImplementationB();
+ testItemB.setAmount(100);
+ testRoot.addTestItem(testItemB);
+
JAXB.marshal(testRoot, System.out);
View
21 unilectures/src/bxt/unilectures/fun/jaxb/TestRoot.java
@@ -1,13 +1,30 @@
package bxt.unilectures.fun.jaxb;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlElementWrapper;
+import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
+import bxt.unilectures.fun.jaxb.list.TestImplementationA;
+import bxt.unilectures.fun.jaxb.list.TestImplementationB;
+import bxt.unilectures.fun.jaxb.list.TestParent;
+
@XmlRootElement(name="test")
public class TestRoot {
private TestBean testBean;
private TestImmutable testImmutable;
+ @XmlElementWrapper(name="items")
+ @XmlElements({
+ @XmlElement(name="a", type=TestImplementationA.class),
+ @XmlElement(name="b", type=TestImplementationB.class),
+ })
+ private List<TestParent> testItems = new ArrayList<TestParent>();
+
public TestBean getTestBean() {
return testBean;
}
@@ -21,4 +38,8 @@ public void setTestImmutable(TestImmutable testImmutable) {
this.testImmutable = testImmutable;
}
+ public void addTestItem(TestParent testItem) {
+ testItems.add(testItem);
+ }
+
}
View
15 unilectures/src/bxt/unilectures/fun/jaxb/list/TestImplementationA.java
@@ -0,0 +1,15 @@
+package bxt.unilectures.fun.jaxb.list;
+
+public class TestImplementationA extends TestParent {
+
+ private String value;
+
+ public String getValue() {
+ return value;
+ }
+
+ public void setValue(String value) {
+ this.value = value;
+ }
+
+}
View
15 unilectures/src/bxt/unilectures/fun/jaxb/list/TestImplementationB.java
@@ -0,0 +1,15 @@
+package bxt.unilectures.fun.jaxb.list;
+
+public class TestImplementationB extends TestParent {
+
+ private int amount;
+
+ public int getAmount() {
+ return amount;
+ }
+
+ public void setAmount(int amount) {
+ this.amount = amount;
+ }
+
+}
View
8 unilectures/src/bxt/unilectures/fun/jaxb/list/TestParent.java
@@ -0,0 +1,8 @@
+package bxt.unilectures.fun.jaxb.list;
+
+import javax.xml.bind.annotation.XmlSeeAlso;
+
+@XmlSeeAlso({TestImplementationA.class,TestImplementationB.class})
+public class TestParent {
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.