`
xiaonu_1225
  • 浏览: 38307 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

jdom 读写xml

XML 
阅读更多
public static synchronized void setPara (String paras) {
    	SAXBuilder builder = new SAXBuilder();    
        Document read_doc = null; 
        File file = null;
        try {    
            String url = UMParas.class.getResource("/").getPath();
            file = new File(url + File.separator + "sysconfig.xml");
            read_doc = builder.build(file);    
        } catch (FileNotFoundException e) {    
            e.printStackTrace();    
        } catch (JDOMException e) {    
            e.printStackTrace();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
        Element rootElement = read_doc.getRootElement(); 
        List list = rootElement.getChildren("para");    
        for (Iterator i = list.iterator(); i.hasNext();) {    
            Element current = (Element) i.next();    
            List item = current.getChildren("item"); 
            for (Iterator iterator = item.iterator(); iterator.hasNext();) {
				Element name = (Element) iterator.next();
				if(name.getAttribute("code").getValue().equals("sametimewebpath")){
					name.setAttribute("value", paras);
	            }
			}
        } 
        try{
            XMLOutputter outputter = new XMLOutputter();
            Format fmt = Format.getPrettyFormat();
            fmt.setIndent("   ");
            outputter.setFormat(fmt);
            outputter.output(rootElement.getDocument(), new FileOutputStream(file.getPath()));  //虽然能接受Writer和OutputStream2个接口,但用writer会产生中文乱码
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
		reloadDom();    
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics