2015-10-02 45 views
0

的XML的例子的XML文件的值:追加到屬性使用Groovy

<response version-api="2.0"> 
    <value> 
     <books> 
      <book available="20" id="1" tags=""> 
       <title>Don Xijote</title> 
       <author id="1" tags="Joel">Manuel De Cervantes</author> 
      </book> 
      <book available="14" id="2" tags"Jane"> 
       <title>Catcher in the Rye</title> 
       <author id="2" tags="">JD Salinger</author> 
      </book> 
      <book available="13" id="3" tags=""> 
       <title>Alice in Wonderland</title> 
       <author id="3">Lewis Carroll</author> 
      </book> 
      <book available="5" id="4" tags="Harry"> 
       <title>Don Xijote</title> 
       <author id="4">Manuel De Cervantes</author> 
      </book> 
     </books> 
    </value> 
</response> 

基本上我想我選擇的字符串值追加到所謂的「標籤」的所有屬性。這是「標籤」屬性是否具有值,也是屬性在xml結構的不同級別。我已經嘗試了方法appendNode(),但吐出了一個常規運行時表達式錯誤。我也看過這個stackoverflow question,但我沒有接近我需要的答案。這是代碼:

def rootFolder = new File('.').getCanonicalPath() 
    def response = new XmlSlurper().parse(new File(rootFolder + File.separator + 'src' + File.separator + 'metadata.xml')) 
    def tags = response.'**'.findAll { it['@tags']!='' } 

    tags.each{ t -> 
     def tagAttr = ([email protected]()) 
     println(tagAttr) 
    } 

任何人有任何想法我可以如何實現這一目標?

回答

0

你近了... ...這應該做你想要的(假設我有棒的右端)什麼:

def xml = '''<response version-api="2.0"> 
      | <value> 
      |  <books> 
      |   <book available="20" id="1" tags=""> 
      |    <title>Don Xijote</title> 
      |    <author id="1" tags="Joel">Manuel De Cervantes</author> 
      |   </book> 
      |   <book available="14" id="2" tags="Jane"> 
      |    <title>Catcher in the Rye</title> 
      |    <author id="2" tags="">JD Salinger</author> 
      |   </book> 
      |   <book available="13" id="3" tags=""> 
      |    <title>Alice in Wonderland</title> 
      |    <author id="3">Lewis Carroll</author> 
      |   </book> 
      |   <book available="5" id="4" tags="Harry"> 
      |    <title>Don Xijote</title> 
      |    <author id="4">Manuel De Cervantes</author> 
      |   </book> 
      |  </books> 
      | </value> 
      |</response>'''.stripMargin() 

import groovy.xml.* 

def parsed = new XmlParser().parseText(xml)   

parsed.'**' 
     .findAll { 'tags' in it.attributes().keySet().toList() } 
     .each { [email protected] += ([email protected] ? ' ' : '') + 'extraTag' } 

println XmlUtil.serialize(parsed) 
+0

非常好,工作!感謝幫助! – CommittedEel

0

您可以使用XmlParser的(而不是的XmlSlurper)。

import groovy.util.XmlParser 

def input = ''' 
<response version-api="2.0"> 
    <value> 
     <books> 
      <book available="20" id="1" tags=""> 
       <title>Don Xijote</title> 
       <author id="1" tags="Joel">Manuel De Cervantes</author> 
      </book> 
      <book available="14" id="2" tags="Jane"> 
       <title>Catcher in the Rye</title> 
       <author id="2" tags="">JD Salinger</author> 
      </book> 
      <book available="13" id="3" tags=""> 
       <title>Alice in Wonderland</title> 
       <author id="3">Lewis Carroll</author> 
      </book> 
      <book available="5" id="4" tags="Harry"> 
       <title>Don Xijote</title> 
       <author id="4">Manuel De Cervantes</author> 
      </book> 
     </books> 
    </value> 
</response> 
''' 

def xml = new XmlParser().parseText(input) 

xml.'**'.findAll { if([email protected] != null) [email protected] = [email protected] + '-hello' } 

findAll()之後,標籤屬性將被修改。