Skip to content

Commit c2cb717

Browse files
committed
Append element example.
1 parent 4eab47c commit c2cb717

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.hisrc.xml.bind.tests.addelement;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
import javax.xml.bind.JAXBContext;
7+
import javax.xml.bind.JAXBException;
8+
import javax.xml.bind.annotation.XmlAttribute;
9+
import javax.xml.bind.annotation.XmlElement;
10+
import javax.xml.bind.annotation.XmlRootElement;
11+
import javax.xml.bind.annotation.XmlTransient;
12+
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
16+
public class AddElementTest {
17+
18+
@Test
19+
public void addsElement() throws JAXBException {
20+
21+
JAXBContext context = JAXBContext.newInstance(Element.class);
22+
final Element element = (Element) context.createUnmarshaller()
23+
.unmarshal(getClass().getResource("element.xml"));
24+
Assert.assertEquals("beta", element.getChildren().get(1).getValue());
25+
}
26+
27+
@XmlRootElement(name = "element")
28+
public static class Element {
29+
private List<Element> children = new LinkedList<Element>();
30+
31+
@XmlTransient
32+
public List<Element> getChildren() {
33+
return children;
34+
}
35+
36+
@XmlElement(name = "element")
37+
public void setChild(Element child) {
38+
this.children.add(child);
39+
}
40+
41+
private String value;
42+
43+
@XmlAttribute
44+
public String getValue() {
45+
return value;
46+
}
47+
48+
public void setValue(String value) {
49+
this.value = value;
50+
}
51+
}
52+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<element>
2+
<element value="alpha" />
3+
<element value="beta" />
4+
</element>

0 commit comments

Comments
 (0)