0

We have a xml which looks like below

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<GeneratorRun>
<param>
    <comGrp name="Abc">            
        <component name="A">
            <genFiles>
                <file location="xyz/a/b/x.h" checksum="1558926677"/>
                <file location="xyz/a/b/y.h" checksum="2621886660"/>                   
            </genFiles>
        </component>
        <component name="B">
            <genFiles>
                <file location=""xyz/a/b/z.h" checksum="1558926677"/>                   
            </genFiles>
        </component>
    </comGrp>
</param>

I need to get all file location information which are under genFiles. WHats the experession for the same.

What I tries so far is below which didn't give the result

GPathResult xmlContent = new XmlSlurper().parse(config.genSourceRootDir.resolve('par.xml').toFile())
  List<String> generatedFiles = xmlContent.'**'.genFiles.file.@location.toList()
user3540481
  • 221
  • 3
  • 13
  • Why do you "look" for `generatedFiles` when you want `genFiles`? – cfrick Sep 02 '21 at 08:29
  • Does this answer your question? [How to find all XML elements by tag name in Groovy?](https://stackoverflow.com/questions/6726592/how-to-find-all-xml-elements-by-tag-name-in-groovy) – cfrick Sep 02 '21 at 08:59
  • That was a typo. Even with genFiles I didnt get the desired result – user3540481 Sep 02 '21 at 09:01

1 Answers1

1

You need to first find the nodes, then query their attributes

something like this:

List<String> generatedFiles = xmlContent.'**'
    .findAll { it.name() == 'genFiles' }
    .file*.collectMany { [it.@location] }.flatten()
tim_yates
  • 167,322
  • 27
  • 342
  • 338