使用 Dom4j 映射复杂结果集
我正在尝试从复杂的结果集(即具有多个联接的结果集)创建 XML 文档,因此竞赛有一堆问题,而一个问题有一堆答案。
因此,该文档最终看起来类似于:
<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>
...
很像 SQL Server 中的“for XML”命令,但我无法使用数据库进行处理,因此它需要使用 Java 语言。谁能给我指出正确的方向,找到任何好的资源,甚至是做这个/类似事情的开源库,这样我就可以让它做我想做的事情?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Calm Storm 所建议的,我将结果集转换为对象树,并在抽象类中创建了自定义 toXml 方法,该方法遍历每个对象中的方法并从中创建 XML 元素和属性。
我简直不敢相信没有一个更简单的开源解决方案可以自动完成这一切!也许我只是懒:)
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 :)
一个相当简单的事情是首先将结果集转换为竞赛对象,然后考虑将该对象转换为 XML(使用 xstream 或某些此类框架)。
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).