Servlet无法从XML获取更新的值

时间:2016-03-23 06:47:50

标签: javascript java xml selenium servlets

在Java Web Project中,我们使用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"));
}

来自webinterface的新值通过&#34; bookingsBOPeriodID&#34; 。在此方法之后,新值不会反映在XML

1 个答案:

答案 0 :(得分:1)

在从xml读取数据之前,让代码等待一段时间。 您应该让资源和流程同步以进行此类操作。