简体   繁体   English

Servlet 无法从 XML 获取更新的值

[英]Servlet Unable to Fetch the updated value from XML

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.在 Java Web 项目中,我们使用 Servlet 使用来自网页的值更新 XML 标记值,然后为了进一步执行,Servlet 必须从 XML 获取更新的标记值并继续,但它正在检索旧值(在更新之前)并继续。

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" .来自 webinterface 的新值通过 "bookingsBOPeriodID" 传递。 Immediately after this method the new value is not reflecting in the XML在此方法之后,新值未立即反映在 XML 中

Make your code wait for sometime before reading the data from the xml.在从 xml 读取数据之前,让您的代码等待一段时间。 You should have your resources and processes Synchronized for such opertaions.您应该为此类操作同步您的资源和进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM