Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a declarative way to parse XML to Java objects?

I'm writing an import function of XML files to my Java application. I am using XOM to parse the XML files. The code for parsing the XML is not easy to understand, it is some hardcoded .getChild(3) and so on. It is hard to follow the code compared to the declarative XML document.

Isn't there a more maintainable way to parse XML documents to Java objects? I would like to have it in a more declarative way, where I can specify what tags corresponds to what Java classes.

like image 460
Jonas Avatar asked Aug 16 '10 16:08

Jonas


1 Answers

Have a look at JAX/B - fairly simple annotation-based approach. It's a standard Java API.

There are tools to generate Annotated Java classes from XSDs or sample XML files. I describe my use of it in my blog

like image 166
djna Avatar answered Sep 21 '22 09:09

djna