如何将查询结果一般性地映射到生成的 XML?
这是查询:
Select id, title, value, old_title, old_value, new_title, new_value FROM VALUES
This the XML:
<row>
<id>
<title>
<value>
<old_state>
<title>
<value>
</old_state>
<new_state>
<title>
<value>
</new_state>
<row>
我对现有 XML 执行此操作的方式是,我没有 new_state
/old_state
,因此我可以只使用结果,例如。 String colName = meta.getColumnLabel(i + 1);
并且无需将 old_title
映射到 title
。现在我唯一想到的是制作一个映射,其中键是计数器值,值是新元素名称,就像这样
boolean columnNameIsStartElement = (columStartNames.get(i) != null)?true=false;
boolean columnNameIsEndElement = (columEndNames.get(i) != null)?true=false;
if (columnNameIsStartElement ){
writer.writeStartElement(columStartNames.get(i))
} else if (columnNameIsEndElement){
writer.writeEndElement(columStartNames.get(i))
}
//continue processing the element as usual
但我不喜欢它。我更喜欢以某种方式使用 XSD,这样解决方案更加通用,并且不需要我计算元素的行数,这样我就可以正确地映射它们(更不用说如果子元素具有额外的孩子,我该如何映射呢?:))
This is the query:
Select id, title, value, old_title, old_value, new_title, new_value FROM VALUES
This the XML:
<row>
<id>
<title>
<value>
<old_state>
<title>
<value>
</old_state>
<new_state>
<title>
<value>
</new_state>
<row>
The way I do it for existing XML is that I don't have the new_state
/old_state
so I can just use the result, eg. String colName = meta.getColumnLabel(i + 1);
and there is no need to map old_title
to title
. Now I have The only thing that comes to mind is to make a map in which the key would be the counter value and the value would be the new element name, like this
boolean columnNameIsStartElement = (columStartNames.get(i) != null)?true=false;
boolean columnNameIsEndElement = (columEndNames.get(i) != null)?true=false;
if (columnNameIsStartElement ){
writer.writeStartElement(columStartNames.get(i))
} else if (columnNameIsEndElement){
writer.writeEndElement(columStartNames.get(i))
}
//continue processing the element as usual
But I don't like it. I would prefer to use the XSD somehow so the solution is more generic and it doesn't require me to count the lines of elements so I can map them correctly (not to mention the problems I'll get into if a child element has an additional child, how could I map that? :))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Castor 将查询结果绑定到 XML 中。看一下这个例子:http://www.castor.org/jdo。 html#使用 JDO 和 XML
You could use Castor to bind your query result into XML. Look at this example: http://www.castor.org/jdo.html#Using-JDO-And-XML
看看XStream,它可以轻松地进行这种映射。
编辑:好的,如果您对输出格式非常严格,并且不需要中间对象,那么您可以考虑使用 Velocity。您只需要从示例输出中编写一个小模板,然后调用velocity,将其传递到上下文中的ResultSet。
Take a look at XStream, it allows to do that kind of mappings easily.
Edit: Ok, so if you're that strict on output format, and you don't want intermediate objects then you could consider using Velocity. You'd just need to write a small template from your sample output, and call velocity passing it your ResultSet in its Context.