/** * * @param 普通版list 参数列表 */ public void writeptParaList(ArrayList list) { try { String sCaseName=""; String sItem=""; String sCycName=""; HashMap map=null; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element element = root; Element cycNode=null; map=(HashMap)list.get(0); //参数文件map sItem="Description"; sCaseName=(String)map.get(sItem); mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, "Description=" + sCaseName ); sItem="CysName"; sCycName=(String)map.get(sItem); map=(HashMap)list.get(1); //public 参数map cycNode=writeptPublicNode(element,map,sCycName); if( writeCycNode(cycNode,list)==false) //循环 参数 System.err.println(" write cyc node fail"); XMLOutputter outputter = new XMLOutputter("", false, "GB2312"); PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(xmlFileName))); Document myDocument = root.getDocument(); outputter.output(myDocument, out); out.close(); } catch (JDOMException jdome) { jdome.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
//改写XML文件的某一元素的值 //strRoute为XML文件从根元素开始到该元素的父元素的路径 //strElement为需要读取的元素 //flag为当XML含有多个同名元素时,需要改写元素所处的序号 public void write(String strRoute, String strElement, String strSet, int flag) { //SAXBuilder builder=new SAXBuilder(); try { String str = null; Document doc = builder.build(xmlFileName); Element root = doc.getRootElement(); Element element = root; StringTokenizer st = new StringTokenizer(strRoute, ":"); str = st.nextToken(); while (st.hasMoreTokens()) { str = st.nextToken(); //mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,str); element = element.getChild(str); } // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"test :"+str); element = (Element) element.getParent(); //若需要改写的不是XML文件的第一个同名元素,则获取该元素 //方法为:将之前的元素依次移到后面,需要改写的元素前移至第一个 /* * if(flag>1) { int j=flag; while(j!=1) { * * Element tmp=element.getChild(str); * element.addContent(tmp.detach()); j--; } } */ element = element.getChild(str).getChild(strElement); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP,"gettxt // "+element.getText()); element.setText(strSet); //若需要改写的不是XML文件的第一个同名元素 //上面改变了次序,需要在成功改写后恢复原有次序 /* * if(flag!=1) { * * java.util.List children=element.getChildren(); Iterator * iterator=children.iterator(); int count=0; * while(iterator.hasNext()) { Element * child=(Element)iterator.next(); count++; } * * System.out.println("count"+count); * * k=(count+1-flag)%count; * * while(k!=0) { Element tmp=element.getChild(str); * element.addContent(tmp.detach()); k--; } } * */ XMLOutputter outputter = new XMLOutputter("", false, "GB2312"); PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(xmlFileName))); Document myDocument = root.getDocument(); outputter.output(myDocument, out); out.close(); } catch (JDOMException jdome) { jdome.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, xmlFileName // + " is not well-formed\n"); } catch (IOException ioe) { ioe.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, ioe); } catch (Exception e) { e.printStackTrace(); // mypage.FcfeMain.wR(mypage.FcfeMain.SIM_APP_ERP, // "write not succeed\n"); } }