我试图将 20 多个 xml 文件合并为一个,其中任何一个都包含 7000 多个节点代码,我为此使用的代码是这样的
private void createFile(String month, String year) throws Exception {
Properties prop = new Properties();
prop.load(new FileInputStream("Config.properties"));
String edb = prop.getProperty("EDB");
String bbbCode = prop.getProperty("BBB");
String InFolder = prop.getProperty("IN");
String OutFolder = prop.getProperty("OUT");
String Particija = prop.getProperty("DISK");
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
Date date = new Date();
String ImeFile = "O3" + dateFormat.format(date) + "0000001.UJP." + bbbCode + ".xml";
String MesecGodina = month + "/" + year;
DateFormat dateTimeFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date dateTime = new Date();
String DatPratka = dateTimeFormat.format(dateTime);
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("UplatiIsplatiMesecIzvestaj");
doc.appendChild(rootElement);
// PrireduvacVodecki elements
Element PrireduvacVodecki = doc.createElement("PrireduvacVodecki");
rootElement.appendChild(PrireduvacVodecki);
// edbElement elements
Element edbElement = doc.createElement("O3_Edb");
edbElement.appendChild(doc.createTextNode(edb));
PrireduvacVodecki.appendChild(edbElement);
// imeFileElement elements
Element imeFileElement = doc.createElement("O3_ImeFile");
imeFileElement.appendChild(doc.createTextNode(ImeFile));
PrireduvacVodecki.appendChild(imeFileElement);
// mesecGodinaElement elements
Element mesecGodinaElement = doc.createElement("O3_MesecGodina");
mesecGodinaElement.appendChild(doc.createTextNode(MesecGodina));
PrireduvacVodecki.appendChild(mesecGodinaElement);
// datPratkaElement elements
Element datPratkaElement = doc.createElement("O3_DatPratka");
datPratkaElement.appendChild(doc.createTextNode(DatPratka));
PrireduvacVodecki.appendChild(datPratkaElement);
// rbrPratkaElement elements
Element rbrPratkaElement = doc.createElement("O3_RbrPratka");
rbrPratkaElement.appendChild(doc.createTextNode("0000001"));
PrireduvacVodecki.appendChild(rbrPratkaElement);
File dir = new File(Particija + ":\\" + InFolder + "\\");
File[] rootFiles = dir.listFiles();
int stavki = 0;
for (File rootFile : rootFiles) {
display.setText(display.getText() + rootFile + "\n");
InputStream in = new FileInputStream(rootFile);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document docc = db.parse(new InputSource(in));
docc.getDocumentElement().normalize();
int y = docc.getElementsByTagName("UplataIsplata").getLength();
display.setText(display.getText() + Integer.toString(y) + "\n");
stavki = stavki + y;
for (int i = 0; i < y; i++) {
// UplataIsplata elements
Element UplataIsplata = doc.createElement("UplataIsplata");
rootElement.appendChild(UplataIsplata);
Element idElement = doc.createElement("O3_Id");
idElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Id").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(idElement);
Element sifraUplMestoElement = doc.createElement("O3_Sifra_uplmesto");
sifraUplMestoElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Sifra_uplmesto").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(sifraUplMestoElement);
Element sifraKompElement = doc.createElement("O3_Sifra_komp");
sifraKompElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Sifra_komp").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(sifraKompElement);
Element serNumTicketElement = doc.createElement("O3_Serial_num_ticket");
serNumTicketElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Serial_num_ticket").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(serNumTicketElement);
Element codeTicketElement = doc.createElement("O3_Code_ticket");
codeTicketElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Code_ticket").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(codeTicketElement);
Element vremeUplataElement = doc.createElement("O3_Vreme_uplata");
vremeUplataElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Vreme_uplata").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(vremeUplataElement);
Element iznosUplataElement = doc.createElement("O3_Iznos_uplata");
iznosUplataElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Iznos_uplata").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(iznosUplataElement);
Element iznosMoznaDobivkaElement = doc.createElement("O3_Iznos_mozna_dobivka");
iznosMoznaDobivkaElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Iznos_mozna_dobivka").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(iznosMoznaDobivkaElement);
Element iznosIsplatenElement = doc.createElement("O3_Iznos_isplaten");
iznosIsplatenElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Iznos_isplaten").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(iznosIsplatenElement);
Element brojKombinaciiElement = doc.createElement("O3_Broj_kombinacii");
brojKombinaciiElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Broj_kombinacii").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(brojKombinaciiElement);
Element vkupenKoefElement = doc.createElement("O3_Vkupen_koef");
vkupenKoefElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Vkupen_koef").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(vkupenKoefElement);
Element vidTransakcijaElement = doc.createElement("O3_Vid_transakcija");
vidTransakcijaElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Vid_transakcija").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(vidTransakcijaElement);
Element statusTicketElement = doc.createElement("O3_Status_ticket");
statusTicketElement.appendChild(doc.createTextNode(docc.getElementsByTagName("O2_Status_ticket").item(i).getChildNodes().item(0).getNodeValue()));
UplataIsplata.appendChild(statusTicketElement);
}
}
// vkBrStavkiElement elements
Element vkBrStavkiElement = doc.createElement("O3_VkBrStavki");
vkBrStavkiElement.appendChild(doc.createTextNode(Integer.toString(stavki)));
PrireduvacVodecki.appendChild(vkBrStavkiElement);
// 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(Particija + ":\\" + OutFolder + "\\" + ImeFile));
transformer.setOutputProperty(OutputKeys.ENCODING, "WINDOWS-1251");
transformer.transform(source, result);
JOptionPane.showMessageDialog(mainPanel, "File Saved!", "XML Merge Information", JOptionPane.INFORMATION_MESSAGE);
jButton1.setEnabled(true);
mainPanel.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.DEFAULT_CURSOR));
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
但经过一段时间的创建文件程序停止工作。如何更改此代码以部分而不是从内存中写入文件