gpt4 book ai didi

java - 将大型 SQLite 表转换为 XML

转载 作者:太空宇宙 更新时间:2023-11-04 12:36:13 25 4
gpt4 key购买 nike

我正在尝试将大型(-ish)表转换为 XML。为此,我使用 DOM 解析器,基于 Mkyong does here .

这对于较小的表来说效果很好,但我在处理具有 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();
}
}
}

最佳答案

谢谢您的提示,我使用了 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();
}

关于java - 将大型 SQLite 表转换为 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37305264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com