收藏 分销(赏)

SAXReader用法.doc

上传人:仙人****88 文档编号:9234016 上传时间:2025-03-18 格式:DOC 页数:5 大小:41KB 下载积分:10 金币
下载 相关 举报
SAXReader用法.doc_第1页
第1页 / 共5页
SAXReader用法.doc_第2页
第2页 / 共5页


点击查看更多>>
资源描述
· SAXReader用法 - [技术]  1.怎样获取环境变量     这个以前转载过一段程序,详见 · 好用的获取环境变量的java静态方法 - [技术] Tag:java 环境变量 获取 方法 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明     public static String getEnv(String envKey) {         Properties prop = new Properties();         try {          String OS = System.getProperty("os.name").toLowerCase();          Process p = null;          //get system environment          if (OS.indexOf("windows") > -1) {           p = Runtime.getRuntime().exec("cmd /c set");          }          BufferedReader br = new BufferedReader(new InputStreamReader(p            .getInputStream()));          String line;          while ((line = br.readLine()) != null) {           int i = line.indexOf("=");           if (i > -1) {            String key = line.substring(0, i);            String value = line.substring(i + 1);            prop.setProperty(key, value);           }          }          return prop.getProperty(envKey);         } catch (Exception e) {          return null;         }        } ***************************************************************** /** * 获取环境变量的java静态方法 * 获得主机操作系统的名称 "windows 2003" * Process 进程 * 如果是windows系统,获得系统下的环境变量 * 将环境变量写入流,循环读取key 和 value * 把key 和 value放进Properties,作为属性封装 * @param envKey * @return */ public static String getEnv(String envKey) { Properties prop = new Properties(); try { String OS = System.getProperty("os.name").toLowerCase();//主机操作系统的名称 Process p = null; // get system environment if (OS.indexOf("windows") > -1) { p = Runtime.getRuntime().exec("cmd /c set"); } BufferedReader br = new BufferedReader(new InputStreamReader(p .getInputStream())); String line; while ((line = br.readLine()) != null) { // System.out.println("br.readLine()========"+br.readLine()); // System.out.println("line========"+line); int i = line.indexOf("="); if (i > -1) { String key = line.substring(0, i); String value = line.substring(i + 1); prop.setProperty(key, value); // System.out.println("key======"+key); // System.out.println("value======"+value); } } return prop.getProperty(envKey); } catch (Exception e) { return null; } ***************************************************************************************** 2. 怎样根据模板生成一个XML文件(根据xml模板文件在指定路径下生成新的xml文件) 需要用到如下两个jar包(dom4j-1.6.1和jaxen-1.1-beta-6)       private void genXMLFile(){         try {             String nwdir = “C:\\workDir”;//指定一个输出路径                       SAXReader reader = new SAXReader(); //使用SAXReader 解析器                      InputStream isrt = this.getClass().getResourceAsStream("templ.xml"); //读取当前路径下的xml模板文件,在jar包下运行也没问题!                    Document doc = reader.read(isrt);                List projects=doc.selectNodes("/preferences/category/preference");//快速定位xml中的节点             Iterator it=projects.iterator();                            while(it.hasNext()){                 Element elm=(Element)it.next();                 Attribute attribute=elm.attribute("name");                 String nodename = attribute.getValue();                                 if(nodename.equals("CAE_batch_meshing_root_dir")){                                       updateVal(elm,nwdir);                           }                                                  }               OutputFormat format = new OutputFormat(" ",   true);//使用format可以换行排版               XMLWriter writer = new XMLWriter(new FileWriter(nwdir+"\\Preferences.xml"),format);//生成新的xml文件               writer.write(doc);               writer.close();                     } catch (DocumentException e) {             // TODO Auto-generated catch block             e.printStackTrace();         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }     }         private void updateVal(Element elm,String newStr){         List nodes = elm.elements("context");//context为xml中的节点         for (Iterator i = nodes.iterator(); i.hasNext();) {            Element e = (Element) i.next();            List node = e.elements("value");            Element end=(Element)node.iterator().next();            end.setText(newStr);//更新节点的值         }     }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服