简体   繁体   中英

Equivalent of “For XML” in MS Access SQL

I need to select records from an MS Access database to an XML output, based on some predefined conditions, from my Java application using JDBC & SQL statements.

Is there a way to achieve that?

Access SQL does not have an equivalent to T-SQL's FOR XML clause. However, you can add a class like this to your project

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();
    }
}

and then pass your ResultSet to the asXml method

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

For a sample table like this ...

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

... the resulting xml string will look like this (formatted for readability)

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM