读取 xml 名称值对不适用于 groovy
read xml name value pair not work with grovvy
我有一个 xml 内容文本文件:
<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>
我想用下面的代码读出名称值对:
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
}
名称正确读出但值为空:
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
使用文本()
println(prop.@name + " => " + prop.text())
versions.put(prop.@name, prop.text())
我有一个 xml 内容文本文件:
<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>
我想用下面的代码读出名称值对:
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
}
名称正确读出但值为空:
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
使用文本()
println(prop.@name + " => " + prop.text())
versions.put(prop.@name, prop.text())