def xmltxt = """
<file>
<something>
<record>
<name>nakina</name>
</record>
<record>
<name>buaboe</name>
</record>
</something>
</file>"""
def xml = new XmlSlurper().parseText(xmltxt)
def fullPath = 'file.something.record'
def pathElements = fullPath.tokenize('.')
pathElements -= xml.name()
def root = xml
pathElements.each {node ->
root = root."$node"
}
return root.size()
You could also use split, as John Wagenleitner suggested in response to my comment on his answer on StackOverflow:
def xmltxt = """
<file>
<something>
<record name="some record" />
<record name="some other record" />
</something>
</file>"""
def xml = new XmlSlurper().parseText(xmltxt)
String foo = "something.record"
def aNode = xml
foo.split("\\.").each {
aNode = aNode."${it}"
}
return aNode.size()
My thanks to you both.
After talking with Josh, I understand why the Groovy people had to do what they did. Dots (.) are still legal in XML element names, as long as the name doesn't start with a dot. (Actually, other punctuation is allowed as well, though it is not recommended). I don't think they could use forward slashes as XPath does because of how Groovy has overloaded the operators and dots had to be allowable, so this is what we're stuck with. Maybe they should have used spaces instead, since those aren't legal in element names. I don't know the impact this would have on other classes.
No comments:
Post a Comment