怎样使用Groovy给XML增加特性
发布时间:2020-12-14 16:48:10 所属栏目:大数据 来源:网络整理
导读:怎样使用Groovy给XML增加特性? 问: 在Groovy中,我需要增加一个特性(attribute)到XML的根元素。我想使用 XmlSlurper。该怎样做?增加元素是很简单。 答: 在Groovy Console 运行以下代码,结果良好。 import?groovy.xml.StreamingMarkupBuilder//?the?ori
怎样使用Groovy给XML增加特性? 问: 在Groovy中,我需要增加一个特性(attribute)到XML的根元素。我想使用 XmlSlurper。该怎样做?增加元素是很简单。 答: 在Groovy Console 运行以下代码,结果良好。 import?groovy.xml.StreamingMarkupBuilder //?the?original?XML def?input?=?"<foo><bar></bar></foo>" //?add?attributeName="attributeValue"?to?the?root def?root?=?new?XmlSlurper().parseText(input) root.@attributeName?=?'attributeValue' //?get?the?modified?XML?and?check?that?it?worked def?outputBuilder?=?new?StreamingMarkupBuilder() String?updatedXml?=?outputBuilder.bind{?mkp.yield?root?} assert?"<foo?attributeName='attributeValue'><bar></bar></foo>"?==?updatedXml 增加一个特性与读一个特性是一样的: import?groovy.xml.StreamingMarkupBuilder def?input?=?''' <thing> ????<more> ????</more> </thing>''' def?root?=?new?XmlSlurper().parseText(input) root.@stuff?=?'new' def?outputBuilder?=?new?StreamingMarkupBuilder() String?result?=?outputBuilder.bind{?mkp.yield?root?} println?result
将生成: <thing?stuff='new'><more></more></thing> 来源:?<http://stackoverflow.com/questions/7795494/how-to-add-xml-attribute-using-groovy> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Inno安装脚本中的HTTP POST请求
- vb.net 的structure结构语自学笔记
- [VB.NET]如何在DataGridView控件中增加、删除和修改记录(用
- perl6 – Perl 6中只有一个参数的构造函数
- 入选Gartner和Forrester报告的AnalyticDB是如何实现PB级数据
- Spring-boot JPA EntityManager注入失败
- 在Delphi中{.$DEFINE VAR}而不是{$DEFINE VAR}的目的是什么
- groovy list转化为数组
- 【未发】大数据分析,Hadoop够用吗?Facebook数据专家说No
- 在public中和在implementation前面定义的全局变量的区别