I need to modify an XML file, overwriting the same file but changing only the 'date' tag to contain the current date. The XML is as follows:
<Document>
<Cst>
<Grp>
<Ini>
<Nm>Uruguay</Nm>
</Ini>
</Grp>
<Pmti>
<Pmt>111111</Pmt>
<PmtM>XXX</PmtM>
<Nb>1</Nb>
<Ctrl>10</Ctrl>
<Fec>2020-07-04</Fec>
<Dbtr>
<Nm>UY</Nm>
<Ps>
<Ctry>UY</Ctry>
</Ps>
</Dbtr>
</Pmti>
</Cst>
</Document>
The file to be generated must contain the same information as the current file and with the same name, only changing the date to the current day. I would like to know how it is done with FileWritter and PrintWritter, which is what I am handling at the moment. Currently I have the following code but I only get the new file to be blank.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
public class AutomRegresionFecha {
public static void main(String[] args) {
try {
Date actual = new Date();
String fecha = new SimpleDateFormat("yyyy-MM-dd").format(actual);
// System.out.println(fecha);
// String newCad = "<ReqdExctnDt>"+fecha+"<ReqdExctnDt>";
//Leer fichero
String linea1 = null;
String path = "C:/Users/user/Desktop/prueba java/base/";
BufferedReader comp1 = null;
File filesBase = new File (path);
File[] listaBase = filesBase.listFiles();
//Crear fichero
FileWriter fichero = null;
PrintWriter pw = null;
for (int b = 0; b < listaBase.length; b++) {
comp1 = new BufferedReader(new FileReader(listaBase[b]));
// System.out.println(listaBase[b].getName());
// fichero = new FileWriter(path + listaBase[b].getName());
// pw = new PrintWriter(fichero);
String linea = "";
while ((linea1 = comp1.readLine()) != null){
String rulo = "";
// System.out.println(linea1);
if(linea1.contains("<ReqdExctnDt>")){
// System.out.println(linea1);
linea = rulo;
rulo = "";
if(linea.indexOf("<Document><Cst><Pmti><Fec>") > -1) {
System.out.println("linea: " + linea);
int inicio = linea.indexOf("<Document><Cst><Pmti><Fec>");
int fin = linea.indexOf("</", inicio + 1);
String reemplazar = linea.substring(inicio + 0, fin);
String uu = linea.replace(reemplazar, "<Document><Cst><Pmti><Fec>"+ fecha);
rulo = uu;
}else rulo = linea;
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}