Question

Having xml:

<root>
  <person id = "123"/>
</root>

Bean mapping:

public class Root {
    private Person person;
    public void setPerson(Person person) ...
}

public class Person {
    String id;
    public void setId(String id) ...
}

I have no idea, how can to implement digester3's pattern for setting id of Person class using its setter (setId(String id)) in such style:

new AbstractRulesModule() {
        @Override
        protected void configure() {
            forPattern("root").createObject().ofType(Root.class);    
            forPattern("root/person").createObject().ofType(Person.class).then().setNext("setPerson");
        }
}
Was it helpful?

Solution

Use callMethod() and callParam(). Your code would be something like this:

new AbstractRulesModule() {
    @Override
    protected void configure() {
        forPattern("root").createObject().ofType(Root.class);
        forPattern("root/person").createObject().ofType(Person.class).then()
                .callMethod("setId").withParamCount(1).then()
                .callParam().ofIndex(0).fromAttribute("id").then()
                .setNext("setPerson");
        }
}

You can consult the javadoc of digester3 to have a better idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top