Groovy解析XML示例
如果你使用过java(不管你是用DOM、或者SAX、JDOM、DOM4J)来解析或者生成xml,那么相比较而言,使用Groovy生成和解析xml就比较容易。 下面单刀直入。 <?xml version="1.0" encoding="UTF-8"?>
<books version="1.0.1" exportDate="2016-10-10 12:30:50">
<comments>This is for groovy xml parse demo</comments>
<operator role="admin">root</operator>
<data>
<book bookid="1" mark="001m">
<name>Netty in action</name>
<price>35</price>
</book>
<book bookid="2" mark="002m">
<name>MyCat in action</name>
<price>25</price>
</book>
<book bookid="3" mark="003m">
<name>MINA in action</name>
<price>4</price>
</book>
<book bookid="4">
<name>The Definitive Guide to SQLite</name>
<price>50</price>
<position url="http://">
<property name="propertiName1">Hello</property>
<property name="propertiName2">World</property>
</position>
</book>
</data>
</books>
Groovy代码 注释:整个代码比较简单,就不一一解释了。 class XmlParseDemo {
public static main(def args){
def books = new XmlParser().parse("books.xml");
def booksData = books.data;
def version = books.attributes().get("version");
def exportDate = books.attributes().get("exportDate");
def externalMsgVer = "version:[" + version + "] exportDate:[" + exportDate +"]"
println externalMsgVer;
println ""
def attrs = books.attributes()
println "books element all attributes"
attrs.each { it ->
println "attributeName:[" + it.key + "],value:[" + it.value +"]"
}
println ""
String comments = books.comments.text();
//it is necessary to use books.operator[0] for specify the first operator element expressly
String operatorRole = books.operator[0].attributes().get("role");
String operatorNull = books.operator[0].attributes().get("NotExist");
String operator = books.operator.text();
def externalMsg1 = "comments:[" + comments + "] operatorRole:[" + operatorRole
def externalMsg2 = externalMsg1 + "] operator:[" + operator + "] NotExist:[" + operatorNull +"]"
println externalMsg2;
int count = 0;
books.data.book.each {
def bookid = it.attributes().get("bookid");
def bookid2 = it.'@bookid';
def mark = it.attributes().get("mark");
String name = it.name[0].text();
String price = it.price[0].text();
def message = "bookid:[" + bookid + "]-[" + bookid2+ "] mark:[" + mark + "] name:[" + name + "] price:[" + price +"]"
println message;
if (it.position[0] != null) {
def properties = it.position[0].property;
properties.each {
println "-------------------"
println it.'@name';
println it.text();
println "-------------------"
}
}
count++;
}
println "Total books number is " + count
println ""
println "book element is accessed by array index"
def specificBook = books.data.book[3];
String tempName = specificBook.name[0].text();
println "book[3] name is " + tempName
}
}
运行结果 books element all attributes comments:[This is for groovy xml parse demo] operatorRole:[admin] operator:[root] NotExist:[null] bookid:[4]-[4] mark:[null] name:[The Definitive Guide to SQLite] price:[50]propertiName1 HellopropertiName2 WorldTotal books number is 4 book element is accessed by array index 截图 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |