Reputation: 1761
I'm trying to create an XML document from a complex result set, i.e. a result set with multiple joins, so a competition has a bunch of questions, and a question has a bunch of answers.
So the document would end up looking something like:
<competitions>
<competition id="12">
<question id="3">
<answer id="34">
The answer
</answer>
<answer id="35">
The answer
</answer>
<answer id="36">
The answer
</answer>
</question>
...
A lot like the "for XML" command in SQL Server, but I can't use the database to do the processing, so it needs to be in Java. Can anyone point me in the right direction to any good resources or even open source libraries that do this / a similar thing so I can make it do what I want please? Thanks.
Upvotes: 0
Views: 552
Reputation: 17631
A reasonably easy thing would be to convert your result set first into a Competitions object then looks at converting the object into XML (using xstream or some such framework).
class Competitions {
List<Competition> getCompetitions();
}
class Competition {
Integer getId();
Question getQuestion();
}
class Question {
Integer getId();
Answer getAnswer();
}
class Answer {
String getText();
}
Upvotes: 0
Reputation: 1761
As Calm Storm suggested, I converted my result sets into an Object tree and created a custom toXml method in an Abstract Class, which traversed the methods in each object and created XML elements and attributes from them.
I just can't believe there isn't an easier Open Source solution for this, that would do all this automatically! Maybe I'm just lazy :)
Upvotes: 0