groovy 之 对xml的解析 和 对文件的读取
2012-08-24 13:17
441 查看
Groovy 是 Java 平台上设计的面向对象编程语言。这门动态语言拥有类似 Python、Ruby 和 Smalltalk 中的一些特性,可以作为 Java 平台的脚本语言使用。Groovy 的语法与 Java 非常相似,以至于多数的 Java 代码也是正确的 Groovy 代码。Groovy 代码动态的被编译器转换成 Java 字节码。由于其运行在 JVM 上的特性,Groovy 可以使用其他 Java 语言编写的库。
对文件的解析
1.创建customers.xml文件,文件内容如下:<?xml version="1.0" encoding="UTF-8"?>
<customers>
<corporate>
<customer name="bill gates" company="microsoft"></customer>
<customer name="steve jobs" company="apple"></customer>
<customer name="bill dyh" company="sun"></customer>
</corporate>
<consumer>
<customer name="jone Doe"></customer>
<customer name="jane Doe"></customer>
</consumer>
</customers>2.创建.groovy的类 FileOpration.groovy,代码如下 (注意:代码和xml文件放在同一个包下)/**
* @author yangy
* Aug 21, 2012
*/
public class FileOpration{
static void main(String[]args){
/* 对file文件进行读取操作*/
def path = FileOpration.class.getResource("").path;
def number = 0
//对xml文件进行解析
new File(path+'customers.xml').eachLine{
line ->
number++
println "$number:$line"
}
/*递归 输出所有的文件名称*/
new File('.').eachFileRecurse{
println it
}
}
}
哦,对了,忘记了groovy的每行代码可以不写分号的,当然加上也可以
下面的是对xml的解析,我感觉相当犀利,groovy内部可能对其做了很强大的优化,这是后话了,上代码1. xml文件还是上面的文件2.创建XmlOpration.groovy类,对xml文件进行解析,代码如下:/**
* @author yangy
* Aug 21, 2012
*/
public class XmlOpration{
static void main(String[]args){
/*对xml进行读取操作*/
def path = XmlOpration.class.getResource("").path;
def customers = new XmlSlurper().parse(new File(path +"customers.xml"))
/*对文件进行解析*/
for(customer in customers.corporate.customer){
println "${customer.@name} works for${customer.@company}";
}
}
}
对xml的解析和对文件的读取就到此为止了,当然可以写一些封装的方法,来供自己的项目调用也是很不错的,可以弄成一个强大的类库,之后会写对string/list的一些使用。
对文件的解析
1.创建customers.xml文件,文件内容如下:<?xml version="1.0" encoding="UTF-8"?>
<customers>
<corporate>
<customer name="bill gates" company="microsoft"></customer>
<customer name="steve jobs" company="apple"></customer>
<customer name="bill dyh" company="sun"></customer>
</corporate>
<consumer>
<customer name="jone Doe"></customer>
<customer name="jane Doe"></customer>
</consumer>
</customers>2.创建.groovy的类 FileOpration.groovy,代码如下 (注意:代码和xml文件放在同一个包下)/**
* @author yangy
* Aug 21, 2012
*/
public class FileOpration{
static void main(String[]args){
/* 对file文件进行读取操作*/
def path = FileOpration.class.getResource("").path;
def number = 0
//对xml文件进行解析
new File(path+'customers.xml').eachLine{
line ->
number++
println "$number:$line"
}
/*递归 输出所有的文件名称*/
new File('.').eachFileRecurse{
println it
}
}
}
哦,对了,忘记了groovy的每行代码可以不写分号的,当然加上也可以
下面的是对xml的解析,我感觉相当犀利,groovy内部可能对其做了很强大的优化,这是后话了,上代码1. xml文件还是上面的文件2.创建XmlOpration.groovy类,对xml文件进行解析,代码如下:/**
* @author yangy
* Aug 21, 2012
*/
public class XmlOpration{
static void main(String[]args){
/*对xml进行读取操作*/
def path = XmlOpration.class.getResource("").path;
def customers = new XmlSlurper().parse(new File(path +"customers.xml"))
/*对文件进行解析*/
for(customer in customers.corporate.customer){
println "${customer.@name} works for${customer.@company}";
}
}
}
对xml的解析和对文件的读取就到此为止了,当然可以写一些封装的方法,来供自己的项目调用也是很不错的,可以弄成一个强大的类库,之后会写对string/list的一些使用。
相关文章推荐
- Java眼中的XML--文件读取--1 应用DOM方式解析XML
- xml解析(读取xml,保存文件到xml)
- [libexpat]_[XML处理]_[C/C++使用libexpat库以数据流(SAX模型)的方式读取(解析)大XML文件]
- Java用jdom.jar解析读取XML文件信息
- C++TinyXml读取解析xml文件
- Dom4j解析XML来读取数据库配置文件信息
- android_xml解析之 使用PULL读取XML文件 .
- 分享非常有用的Java程序 (关键代码)(六)---解析/读取XML 文件(重要)
- dom4j解析(读取)xml文件
- 读取配置文件[方式三]之xml解析配置文件法
- boost::property_tree读取解析.xml文件
- boost::property_tree读取解析.xml文件
- Java眼中的XML--文件读取--2 应用SAX方式解析XML
- [转载]JAVA高手解析XML配置文件的读取操作
- Xml解析、properties文件读取
- Android入门学习笔记(一)|基础知识|文件数据存储读取|解析XML
- .net关于xml文件命名空间的解析读取
- 解析/读取XML 文件
- groovy构建和解析xml文件
- Cocos2d-x XML文件读取操作与解析操作