我是groovy的新手。我试图运行这个时髦的脚本:
def inxml = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <doc><extension source='ExtractTextStage'>" +
"<field name='DC.Date.Modified'>2006-04-13</field><field name='dc.date'>01-01-2016</field><field name='dc.language'>EN</field></extension>"+
"<extension source='you'>" +
"<field name='dc.date'>02-02-2015</field><field name='dc.language'>EN</field></extension></doc>"
def doc = new XmlSlurper().parseText(inxml)
def date = doc.extension.find{ extension-> extension.@source='ExtractTextStage'}.field.find { field->field.@name == "DC.Date.Modified" }
doc.add("last_modified", date)
print doc;
但我收到此错误:
groovy.lang.MissingMethodException: No signature of method: groovy.util.slurpersupport.NodeChild.add() is applicable for argument types: (java.lang.String, groovy.util.slurpersupport.NodeChild) values: [last_modified, 2006-04-13]
Possible solutions: any(), wait(), name(), pop(), min(), tail()
at Script1.run(Script1.groovy:10)
我想添加一个名为&#34; last_modified&#34;的新字段。在我的XML中,并为其分配与&#34; DC.Date.Modified&#34;相同的值。场价值。任何帮助将不胜感激。
答案 0 :(得分:2)
认为你需要做这样的事情:
def inxml = '''<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|<doc>
| <extension source='ExtractTextStage'>
| <field name='DC.Date.Modified'>2006-04-13</field>
| <field name='dc.date'>01-01-2016</field>
| <field name='dc.language'>EN</field>
| </extension>
| <extension source='you'>
| <field name='dc.date'>02-02-2015</field>
| <field name='dc.language'>EN</field>
| </extension>
|</doc>'''.stripMargin()
def doc = new XmlSlurper().parseText(inxml)
def date = doc.extension
.find { extension -> extension.@source='ExtractTextStage'}
.field
.find { field -> field.@name == "DC.Date.Modified" }
def newNode = new XmlSlurper().parseText("<last_modified>$date</last_modified>")
doc.appendNode(newNode)
println groovy.xml.XmlUtil.serialize(doc)