RT。工作中频繁需要新建xml文件,格式大部分相同,例如里面root标签都是和xml文件名称一致的,
如何配置模板?
尝试了 %{file_name}等各种变量不行;另外,insert variable 中并没有
获取当前文件名的方式。如下图:
请诸位不吝赐教。
http://song-ps.iteye.com/blog/369459
看了下Eclipse模版相关的jar是在Eclipse文件包中的plugin文件夹中搜索org.eclipse.jdt.ui.XX.jar这个是与模版有关联的类。
定义的模版的全局变量如下,xml模版中能使用的变量也只有这几个。即你贴出图的insert variable...列表显示的变量
# global variables
GlobalVariables.variable.description.cursor=The cursor position after editing template variables
GlobalVariables.variable.description.dollar=The dollar symbol
GlobalVariables.variable.description.date=Current date
GlobalVariables.variable.description.year=Current year
GlobalVariables.variable.description.time=Current time
GlobalVariables.variable.description.user=User name
GlobalVariables.variable.description.selectedWord= The selected word
GlobalVariables.variable.description.selectedLines= The selected lines
没有filename这个变量。但是Java的code template模版中有filename这个变量。如果要自定义全局filename则需要研究下CodeTemplate是如何实现这个变量的。而且还比较复杂。参考资料:http://blog.csdn.net/id19870510/article/details/8127138
最简单的办法是用Java代码生成指定格式的xml文件。祝好!
用Java代码实现你需要的xml文件模版的示例代码如下:生成文件在当前文件夹内。
public static void genarateXmlFile(String filename){
StringBuffer content = new StringBuffer();
content.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
content.append("<"+filename+">");
content.append("\n\r");
content.append("");
content.append("</"+filename+">");
FileUtil.write("src/"+filename+".xml", content.toString(), false);
}
/**
* 将字符串以指定的编码方式写入文件
* @param fileName 待写入的文件
* @param content 待写入的文本
* @param append 是否追加
* @param charset 文件输出编码格式,默认为UTF-8
*/
public static void write(String fileName, String content,boolean append,String charset){
if(fileName==null||"".equals(fileName)){
return;
}
if(content==null||"".equals(content)){
return;
}
//默认编码格式为UTF-8
if(charset==null){
charset = "UTF-8";
}
//将字符串写入到文件输出流中
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(fileName, append), charset);
osw.write(content + "\r\n");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我记得,我以前用java代码写的