Home > Blockchain >  read xml name value pair not work with grovvy
read xml name value pair not work with grovvy

Time:04-30

I have a xml content text file:

<resources>
  <string name='abc'>3.0</string>
  <string name='def'>4.0</string>
  <string name='ghi'>5.0</string>
  <string name='jkl'>2.0</string>
  <string name='mno'>1.0</string>
  <string name='pqr'>2.0</string>
  <string name='stu'>5.0</string>
  <string name='vwx'>2.0</string>
</resources>

I would like to read out the name value pair with below code:

def loadVersions(verFile) {

    def parsedXml = new XmlParser().parse(verFile)
    def versions = [:]
    parsedXml.each{ prop ->
        println(prop)
        println(prop.@name   " => "   prop.@value)
        versions.put(prop.@name, prop.@value)
    }
    print(versions)
    return versions
}

The name read out properly but value is null:

string[attributes={name=abc}; value=[3.0]]
abc => null
string[attributes={name=def}; value=[4.0]]
def => null
string[attributes={name=ghi}; value=[5.0]]
ghi => null
string[attributes={name=jkl}; value=[2.0]]
jkl => null
string[attributes={name=mno}; value=[1.0]]
mno => null
string[attributes={name=pqr}; value=[2.0]]
pqr => null
string[attributes={name=stu}; value=[5.0]]
stu => null
string[attributes={name=vwx}; value=[2.0]]
vwx => null

CodePudding user response:

use text()

        println(prop.@name   " => "   prop.text())
        versions.put(prop.@name, prop.text())
  • Related