生成和解析xml的利器
2013-09-28 09:17
85 查看
import groovy.xml.MarkupBuilder
//该文件用于生成本地xml配置文件
def toDimens(String filePath,String targetPath) {
def file = new File(targetPath)
if (file.exists())
file.delete()
def writer = file.newPrintWriter()
def xml = new MarkupBuilder(writer)
//生成xml文件
writer.append('<?xml version="1.0" encoding="utf-8"?> \n')
xml.resources {
new File(filePath).eachLine{line->
bits = line.tokenize("=")
try {
dimen(name:bits[0].trim(),bits[1].trim())
} catch (Exception e) {
//exceptions pah!
}
}
}
writer.flush()
//控制台输出文件信息
println writer.toString()
writer.close()
}
java调用groovy:
/**
* 生成xml文件
*
* @param filePath
* 源文件
* @param targetPath
* 生成文件路径
* @param type
* 生成配置文件的类型
*/
public static void pro2xml(String filePath, String targetPath, int type) {
// System.out.println("============生成xml文件===========");
ClassLoader parent = Utils.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
try {
GroovyObject groovyObject = null;
File tempFile = new File(Constants.GROOVY_PATH);
// 加载类S
Class<?> groovyClass = loader.parseClass(tempFile);
groovyObject = (GroovyObject) groovyClass.newInstance();
System.out.println("-> " + groovyObject.getClass());
if (type == Constants.METHOD_TODIMENS) {
groovyObject.invokeMethod("toDimens", new Object[] { filePath, targetPath });
} else if (type == Constants.METHOD_TOARRAY) {
groovyObject.invokeMethod("toArray", new Object[] { filePath, targetPath });
} else {
groovyObject.invokeMethod("toString", new Object[] { filePath, targetPath });
}
// 执行 groovy方法,生成xml配置文件
GroovyShell gShell = new GroovyShell(parent);
gShell.setVariable("filePath", filePath);
gShell.setVariable("targetPath", targetPath);
gShell.evaluate(tempFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 删除properties文件
// File file = new File(filePath);
// if (file.exists()) {
// file.delete();
// }
}
}
//该文件用于生成本地xml配置文件
def toDimens(String filePath,String targetPath) {
def file = new File(targetPath)
if (file.exists())
file.delete()
def writer = file.newPrintWriter()
def xml = new MarkupBuilder(writer)
//生成xml文件
writer.append('<?xml version="1.0" encoding="utf-8"?> \n')
xml.resources {
new File(filePath).eachLine{line->
bits = line.tokenize("=")
try {
dimen(name:bits[0].trim(),bits[1].trim())
} catch (Exception e) {
//exceptions pah!
}
}
}
writer.flush()
//控制台输出文件信息
println writer.toString()
writer.close()
}
java调用groovy:
/**
* 生成xml文件
*
* @param filePath
* 源文件
* @param targetPath
* 生成文件路径
* @param type
* 生成配置文件的类型
*/
public static void pro2xml(String filePath, String targetPath, int type) {
// System.out.println("============生成xml文件===========");
ClassLoader parent = Utils.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
try {
GroovyObject groovyObject = null;
File tempFile = new File(Constants.GROOVY_PATH);
// 加载类S
Class<?> groovyClass = loader.parseClass(tempFile);
groovyObject = (GroovyObject) groovyClass.newInstance();
System.out.println("-> " + groovyObject.getClass());
if (type == Constants.METHOD_TODIMENS) {
groovyObject.invokeMethod("toDimens", new Object[] { filePath, targetPath });
} else if (type == Constants.METHOD_TOARRAY) {
groovyObject.invokeMethod("toArray", new Object[] { filePath, targetPath });
} else {
groovyObject.invokeMethod("toString", new Object[] { filePath, targetPath });
}
// 执行 groovy方法,生成xml配置文件
GroovyShell gShell = new GroovyShell(parent);
gShell.setVariable("filePath", filePath);
gShell.setVariable("targetPath", targetPath);
gShell.evaluate(tempFile);
} catch (Exception e) {
e.printStackTrace();
} finally {
// 删除properties文件
// File file = new File(filePath);
// if (file.exists()) {
// file.delete();
// }
}
}
相关文章推荐
- XML的解析与生成
- SAX解析和生成XML文档
- dom4j 生成并解析xml文件
- SAX解析和生成XML文档
- Dom4j解析和生成XML文档
- C++中XML的解析与生成之TinyXML
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- Libxml2的学习—生成、解析xml文件
- ORM Querier 基于Transact-SQL解析的代码生成利器 帮助开发人员高效快速生成需要的ORM代码
- 四种生成和解析XML文档的方法详解
- 【Android网络开发の1】XML之DOM方式 解析和生成XML文件 推荐
- Android使用PULL解析和生成XML文件
- 使用XStream解析和生成xml文件
- Android中pull解析和生成xml数据到一个字符串中
- 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)
- dom4j解析和生成XML文件
- Eclipse下jdom配置及用之生成、解析xml文档
- DOM生成和解析xml
- jdom生成与解析xml详解
- 用JavaScript生成Android SDK的下载地址(2)——异步加载、解析xml,得到全部SDK的下载地址