I have a following XML document which I'm going to parse into an object model with Apache Digester parser(via Digester annotations):
<?xml version="1.0" encoding="UTF-8"?>
<Decision>
<Name>Antivirus software for Windows</Name>
<Description>Description 1</Description>
<Url>http://yahoo.com</Url>
<ImageUrl>http://yahoo.com/img.jpg</ImageUrl>
<CriterionGroups>
<CriterionGroup>
<Name>Windows</Name>
<Description>Description 1</Description>
<Criteria>
<Criterion>
<Name>Heuristics</Name>
<Description>Description 1</Description>
</Criterion>
</Criteria>
</CriterionGroup>
</CriterionGroups>
<Criteria>
<Criterion>
<Name>On-demand scan</Name>
<Description>Description 1</Description>
</Criterion>
</Criteria>
<CharacteristicGroups>
<CharacteristicGroup>
<Name>Windows</Name>
<Description>Description 1</Description>
<Characteristics>
<Characteristic>
<Name>Country of origin</Name>
<Description>Description 1</Description>
<ValueType>String</ValueType>
<VisualMode>SelectBox</VisualMode>
<Sortable>true</Sortable>
<Options>
<Option>
<Value>Shareware</Value>
<Description>Description 1</Description>
</Option>
</Options>
</Characteristic>
</Characteristics>
</CharacteristicGroup>
</CharacteristicGroups>
<Characteristics>
<Characteristic>
<Name>License</Name>
<Description>Description 1</Description>
<ValueType>Integer</ValueType>
<VisualMode>Slider</VisualMode>
<Sortable>false</Sortable>
</Characteristic>
</Characteristics>
<Decisions>
<Decision>
<Name>Avast Free Antivirus</Name>
<Description>Description 1</Description>
<Url>http://google.com</Url>
<ImageUrl>http://google.com/img.jpg</ImageUrl>
<Votes>
<Vote>
<CriterionName>On-demand scan</CriterionName>
<Weight>4.3</Weight>
</Vote>
<Vote>
<CriterionName>Heuristics</CriterionName>
<CriterionName>On-demand scan</CriterionName>
<Weight>4.3</Weight>
<Description>Description 1</Description>
</Vote>
</Votes>
<Values>
<Value>
<CharacteristicName>License</CharacteristicName>
<Value>Proprietary</Value>
<Description>Description 1</Description>
</Value>
</Values>
</Decision>
</Decisions>
</Decision>
As you can see from this XML there are two Criterion
nodes by two different paths:
- Decision/Criteria/Criterion
- Decision/CriterionGroups/CriterionGroup/Criteria/Criterion
This my object model:
@ObjectCreate(pattern = "Decision")
public class DecisionNode {
@BeanPropertySetter(pattern = "Decision/Name")
private String name;
@BeanPropertySetter(pattern = "Decision/Description")
private String description;
@BeanPropertySetter(pattern = "Decision/Url")
private String url;
@BeanPropertySetter(pattern = "Decision/ImageUrl")
private String imageUrl;
private List<CriterionGroupNode> criterionGroupNodes = new ArrayList<>();
private List<CriterionNode> criterionNodes = new ArrayList<>();
private List<CharacteristicGroupNode> characteristicGroupNodes = new ArrayList<>();
private List<CharacteristicNode> characteristicNodes = new ArrayList<>();
private List<DecisionNode> decisionNodes = new ArrayList<>();
private List<VoteNode> voteNodes = new ArrayList<>();
private List<ValueNode> valueNodes = new ArrayList<>();
....
@SetNext
public boolean addCriterionGroupNode(CriterionGroupNode criterionGroupNode) {
return criterionGroupNodes.add(criterionGroupNode);
}
....
}
@ObjectCreate(pattern = "Decision/CriterionGroups/CriterionGroup")
public class CriterionGroupNode {
@BeanPropertySetter(pattern = "Decision/CriterionGroups/CriterionGroup/Name")
private String name;
@BeanPropertySetter(pattern = "Decision/CriterionGroups/CriterionGroup/Description")
private String description;
private List<CriterionNode> criterionNodes = new ArrayList<>();
....
@SetNext
public boolean addCriterionNode(CriterionNode criterionNode) {
return criterionNodes.add(criterionNode);
}
....
}
@ObjectCreate(pattern = "Decision/Criteria/Criterion")
public class CriterionNode {
@BeanPropertySetter(pattern = "Decision/Criteria/Criterion/Name")
private String name;
@BeanPropertySetter(pattern = "Decision/Criteria/Criterion/Description")
private String description;
public CriterionNode() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Right now I'm only able to parse Decision/Criteria/Criterion
but Decision/CriterionGroups/CriterionGroup/Criteria/Criterion
are still NULL
. How to configure my model and change annotations in order to be able to parse CriterionNode
with two different locations ?
Also, I don't understand why parser finds two Criterion
nodes instead a single one by Decision/Criteria/Criterion
: