Parse document by template in Java

java, parsing, template-engine, xml

Solution

You might want to look at Apache Digester as well : http://commons.apache.org/proper/commons-digester/guide/core.html - this doesn't use a template-approach , but it looks quite well suited for the problem stated here.

( Originally a comment: but OP stated that this provided a viable solution for them - so moving to answer )

Problem

Is there any ready-to-use java library that, given a template, can read an xml-file that complies with that template and parse its values into a Java class? Sort of the job velocity does but in the reverse direction. For example, given the following template ``` <person> <name>${person.name}</name> <age>${person.age}</age> </person> ``` and the input file ``` <person> <name>John</name> <age>20</age> </person> ``` it could read its values into class ``` class Person { public String name; public Integer age; } ``` UPDATE: The example above is meant to show the general idea and has nothing to do with the serialization. Real example can also have elements and attributes that correspond to the fields related to the different Java object and the input file can have structure which cannot be used to deserialize objects with the values located across different attributes of different XML elements. So it's not a serialization issue.

Original source