加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Groovy解析XML示例

发布时间:2020-12-14 16:43:11 所属栏目:大数据 来源:网络整理
导读:如果你使用过java(不管你是用DOM、或者SAX、JDOM、DOM4J)来解析或者生成xml,那么相比较而言,使用Groovy生成和解析xml就比较容易。 下面单刀直入。 xml文件 ?xml version="1.0" encoding="UTF-8"? books version = "1.0.1" exportDate = "2016-10-10 12:3

如果你使用过java(不管你是用DOM、或者SAX、JDOM、DOM4J)来解析或者生成xml,那么相比较而言,使用Groovy生成和解析xml就比较容易。

下面单刀直入。
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代码
(你可以很容地将文件转为string。 解析文件使用def books = new XmlParser().parse(“books.xml”);, 解析文本使用def books= new XmlParser().parseText(xmlText))

注释:整个代码比较简单,就不一一解释了。

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

    }
}

运行结果
version:[1.0.1] exportDate:[2016-10-10 12:30:50]

books element all attributes
attributeName:[version],value:[1.0.1]
attributeName:[exportDate],value:[2016-10-10 12:30:50]

comments:[This is for groovy xml parse demo] operatorRole:[admin] operator:[root] NotExist:[null]
bookid:[1]-[1] mark:[001m] name:[Netty in action] price:[35]
bookid:[2]-[2] mark:[002m] name:[MyCat in action] price:[25]
bookid:[3]-[3] mark:[003m] name:[MINA in action] price:[4]

bookid:[4]-[4] mark:[null] name:[The Definitive Guide to SQLite] price:[50]

propertiName1

Hello


propertiName2

World

Total books number is 4

book element is accessed by array index
book[3] name is The Definitive Guide to SQLite

截图

这里写图片描述

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读