im getting query result in Web Service and sending it to Client Service I've converted the result to XML by using this:
我在Web服务中获取查询结果并将其发送到客户端服务我已经使用以下结果将结果转换为XML:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url + dbName, userName, password);
Statement st = conn.createStatement();
ResultSet rs;
rs = st.executeQuery("SELECT * FROM information_schema.collations where id like '%84%'");
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
while (rs.next()) {
Element row = doc.createElement("Row");
results.appendChild(row);
for (int i = 1; i <= colCount; i++) {
String columnName = rsmd.getColumnName(i);
Object value = rs.getObject(i);
Element node = doc.createElement(columnName);
node.appendChild(doc.createTextNode(value.toString()));
row.appendChild(node);
}
}
DOMSource domSource = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
StringWriter sw = new StringWriter();
StreamResult sr = new StreamResult(sw);
transformer.transform(domSource, sr);
System.out.println(sw.toString());
conn.close();
rs.close();
} catch (SQLException e) {
throw new RuntimeException("Could not get connection", e);
}
is this considered as a right way? how to send it to client service? how to show it in a table?
这被认为是正确的方式吗?如何将其发送到客户服务?如何在表中显示它?
1 个解决方案
#1
0
If this is the right way, greatly depends on the problem you are trying to solve.
如果这是正确的方法,很大程度上取决于您试图解决的问题。
Usually it is not a very good usage of XML to just map a database table to a XML document in a rather generic way (also make sure that the ISO-8859-1 encoding is really what you want, that is you do not have any special characters in your data and also do not expect this application to run with data covering different languages).
通常,以一种相当通用的方式将数据库表映射到XML文档并不是一个非常好的XML用法(也要确保ISO-8859-1编码真的是你想要的,那就是你没有任何数据中的特殊字符,也不希望此应用程序与包含不同语言的数据一起运行)。
You can achieve a lot more by defining objects with structure and relationships and such. If you already have an object structure representing your data in a better way, there is also some libraries with which you can map those data structures to XML (e.g JAXB or XStream are two often used librariers for XML serialization).
通过定义具有结构和关系的对象等,您可以实现更多目标。如果您已经有一个更好地表示数据的对象结构,那么还有一些库可以将这些数据结构映射到XML(例如,JAXB或XStream是两个常用于XML序列化的库。
Explain more what you try to achieve and you might receive better answers.
解释更多你想要达到的目标,你可能会得到更好的答案。
#1
0
If this is the right way, greatly depends on the problem you are trying to solve.
如果这是正确的方法,很大程度上取决于您试图解决的问题。
Usually it is not a very good usage of XML to just map a database table to a XML document in a rather generic way (also make sure that the ISO-8859-1 encoding is really what you want, that is you do not have any special characters in your data and also do not expect this application to run with data covering different languages).
通常,以一种相当通用的方式将数据库表映射到XML文档并不是一个非常好的XML用法(也要确保ISO-8859-1编码真的是你想要的,那就是你没有任何数据中的特殊字符,也不希望此应用程序与包含不同语言的数据一起运行)。
You can achieve a lot more by defining objects with structure and relationships and such. If you already have an object structure representing your data in a better way, there is also some libraries with which you can map those data structures to XML (e.g JAXB or XStream are two often used librariers for XML serialization).
通过定义具有结构和关系的对象等,您可以实现更多目标。如果您已经有一个更好地表示数据的对象结构,那么还有一些库可以将这些数据结构映射到XML(例如,JAXB或XStream是两个常用于XML序列化的库。
Explain more what you try to achieve and you might receive better answers.
解释更多你想要达到的目标,你可能会得到更好的答案。