等价于MS Access SQL中的“ For XML”

时间:2018-08-14 13:41:12

标签: java sql xml ms-access jdbc

我需要根据一些预定义的条件,从Java Access应用程序中使用JDBC和SQL语句从MS Access数据库中选择记录到XML输出。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:3)

Access SQL没有等效于T-SQL的FOR XML子句。但是,您可以在项目中添加这样的类

package com.example.ucanaccessdemo;

import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class ResultSetToXml {
    // adapted from
    //   http://www.java2s.com/Code/Java/Database-SQL-JDBC/ConvertaResultSettoXML.htm

    public static String asXml(ResultSet rs) throws ParserConfigurationException, SQLException, TransformerException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element results = doc.createElement("Results");
        doc.appendChild(results);

        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);
                if (value != null) {
                    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, "UTF-8");
        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);

        return sw.toString();
    }
}

,然后将您的ResultSet传递给asXml方法

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM Clients");
String xml = ResultSetToXml.asXml(rs);

对于这样的示例表...

ID  LastName      FirstName  DOB
--  ------------  ---------  -------------------
 1  Thompson      Gord       2017-04-01 07:06:27
 2  Loblaw        Bob        1966-09-12 16:03:00
 3  Παπαδόπουλος  Ώπα

...生成的xml字符串看起来像这样(为便于阅读而格式化)

<Results>
    <Row>
        <ID>1</ID>
        <LastName>Thompson</LastName>
        <FirstName>Gord</FirstName>
        <DOB>2017-04-01 07:06:27.0</DOB>
    </Row>
    <Row>
        <ID>2</ID>
        <LastName>Loblaw</LastName>
        <FirstName>Bob</FirstName>
        <DOB>1966-09-12 16:03:00.0</DOB>
    </Row>
    <Row>
        <ID>3</ID>
        <LastName>Παπαδόπουλος</LastName>
        <FirstName>Ώπα</FirstName>
    </Row>
</Results>