We have a Java Web Project where we utilize a Servlet to update a specific XML tag value. The updated value is obtained from a webpage and passed to the Servlet. However, when the Servlet tries to fetch this updated value from the XML for further processing, it retrieves the old value instead of the updated one.
public void setPeriodID(String bookingsBOPeriodID) throws InterruptedException {
try{
final String FilePath=UtilLib.getEnvVar("ConfigXMLFilePath");
String filepath = FilePath;
String bwperiodid=" and ";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filepath);
// Get the staff element by tag name directly
Node Parameters = doc.getElementsByTagName("Parameters").item(0);
// loop the staff child node
NodeList list = Parameters.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
//BookingsBO
if (bookingsBOPeriodID!=null && bookingsBOPeriodID.length()!=0 && "BookingsBOINPeriodId".equals(node.getNodeName()) && bookingsBOPeriodID.indexOf(bwperiodid)==-1 ){
System.out.println("***** Updating Bookings BO IN Period id ********");
System.out.println("inside updateEnvPeriodID::"+bookingsBOPeriodID);
node.setTextContent(bookingsBOPeriodID);
// node.setNodeValue(bookingsBOPeriodID);
}
}
// 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(filepath));
transformer.transform(source, result);
System.out.println("******* Period Id details updated **************");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
System.out.println("in period id after update :"+ UtilLib.getParam("BookingsBOINPeriodId"));
}
The value passed from the web interface is stored in the variable "bookingsBOPeriodID". Despite being passed correctly, the new value does not reflect immediately in the XML after executing this method.