使用 Dom4j 映射复杂结果集

发布于 2024-08-23 11:54:05 字数 551 浏览 4 评论 0原文

我正在尝试从复杂的结果集(即具有多个联接的结果集)创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2024-08-30 11:54:06

正如 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 :)

小嗷兮 2024-08-30 11:54:06

一个相当简单的事情是首先将结果集转换为竞赛对象,然后考虑将该对象转换为 XML(使用 xstream 或某些此类框架)。

class Competitions {
   List<Competition> getCompetitions();
}

class Competition {
   Integer getId();
   Question getQuestion();
}

class Question {
   Integer getId();
   Answer getAnswer();
}

class Answer {
   String getText();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文