将大型SQLite表转换为XML

时间:2016-05-18 16:27:15

标签: java xml sqlite

我正在尝试将大型(-ish)表转换为XML。为此,我根据Mkyong does here的方式使用DOM Parser。

这适用于较小的表,但是我遇到了一个包含7列和大约350万行的表的问题。它只需要永远。你能建议一个更好的方法或指出我可以改进代码的地方吗?

我不允许使用除Java和/或SQLite之外的任何东西。也许我没有使用正确的关键字进行搜索,但到目前为止谷歌还没有找到任何好处。

public class Export {

public static void export() {
    String select = "Select * FROM individuen ORDER BY ID";
    DBController dbc = DBController.getInstance();
    dbc.initDBConnection();

    try {
        Statement stmt = DBController.connection.createStatement();
        ResultSet rs = stmt.executeQuery(select);
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

        // root elements
        Document doc = docBuilder.newDocument();
        Element rootElement = doc.createElement("individuen");
        doc.appendChild(rootElement);

        while (rs.next()) {
            Element person = doc.createElement("Person");
            rootElement.appendChild(person);

            // set attribute to person element
            Attr attr = doc.createAttribute("id");
            attr.setValue(Integer.toString(rs.getInt(1)));
            person.setAttributeNode(attr);

            Element geschlecht = doc.createElement("Geschlecht");
            geschlecht.appendChild(doc.createTextNode(Integer.toString(rs.getInt(2))));
            person.appendChild(geschlecht);

            [...]
        }

        System.out.println("oooooooooooout");
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

        System.out.println("Als XML exportiert!");

    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    } catch (TransformerException tfe) {
        tfe.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
}

1 个答案:

答案 0 :(得分:0)

感谢您的提示,我选择了SAX / StAX,现在工作正常。以下是可能正在寻找类似内容的任何人的代码的一部分:

public void export(Population pop) {
    String select = "Select * FROM individuen ORDER BY ID";
    DBController dbc = DBController.getInstance();
    dbc.initDBConnection();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer;

    try {
        Statement stmt = DBController.connection.createStatement();
        ResultSet rs = stmt.executeQuery(select);
        writer = factory.createXMLStreamWriter(new FileOutputStream("output.xml"));

        // Creating the XML-Header
        writer.writeStartDocument();
        // Root Element
        writer.writeStartElement("individuen");
        while (rs.next()) {

            // Element Person, encompasses the other Elements
            writer.writeStartElement("Peron");
            writer.writeAttribute("ID", rs.getString(1));

            // Element Geschlecht
            writer.writeStartElement("Geschlecht");
            writer.writeCharacters(rs.getString(2));
            writer.writeEndElement();

            // Element Alter
            writer.writeStartElement("Alter");
            writer.writeCharacters(rs.getString(3));
            writer.writeEndElement();

            // Element Alter
            writer.writeStartElement("Familienstand");
            writer.writeCharacters(rs.getString(4));
            writer.writeEndElement();

            // Element PartnerID
            writer.writeStartElement("PartnerID");
            writer.writeCharacters(rs.getString(4));
            writer.writeEndElement();

            // Element Bildungsstand
            writer.writeStartElement("Bildungsstand");
            writer.writeCharacters(rs.getString(5));
            writer.writeEndElement();

            // Closes Element Person
            writer.writeEndElement();
        }
        writer.writeEndDocument();
        writer.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }