In a Java Web Project we are using a Servlet to update an XML tag value using the values coming from a webpage then for further execution the Servlet has to fetch this updated tag value from the XML and proceed, however it is retrieving the old value (before the update) and proceeding.
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"));
}
New value from webinterface is passed through "bookingsBOPeriodID" . Immediately after this method the new value is not reflecting in the XML
Make your code wait for sometime before reading the data from the xml. You should have your resources and processes Synchronized for such opertaions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.