Reputation: 21
Sample XML :
<parent>
<child>1</child>
<child>2</child>
<child>3</child>
</parent>
I'm trying to parse the xml using commons-digester annotation. and I only want to get the first element of the xml. But it always gets the last element from the repetitive elements. May you help me?
here is the sample code:
@ObjectCreate(pattern = "parent")
public class Parent {
@BeanPropertySetter(pattern = "parent/child")
private String child;
public String getChild() {
return child;
}
public void setChild(String child) {
this.child= child;
}
}
Upvotes: 0
Views: 83
Reputation: 2836
Digester will, by default, fire the rule for every child element in order, which is why the last one wins.
It is possible to do something with custom rules, but the simplest solution in your case would seem to be to just update the setter to ignore the child if it is already set:
public void setChild(String child) {
if (this.child == null) {
this.child = child;
}
}
HTH
Upvotes: 0