Question

I'm trying to parse FXCop XML to get a collection of all the "Issues..." tags with the values of their related "Level" attributes. I'm very new to Groovy so I'm stumbling with this one. The schema of the FXCop XML is very complex and the "Issues..." tags can appear at several levels in the schema. I suppose I could "brute force" it and code a separate search at each level but that could ultimately be problematic for more complex programs that are being analyzed.

Is there a simple (relatively) way to retrieve all the "Issues ..." tags from an XML document without having to manually code GPath searches down each of the subtrees?

Any help is greatly appreciated.

Was it helpful?

Solution

Given the xml from your comment:

def x = '''<?xml version="1.0" encoding="utf-8"?>
          |<FxCopReport Version="11.0">
          |    <Namespaces>
          |        <Namespace Name="RWSTestXDT">
          |            <Messages>
          |                <Issue Name="Namespace" Certainty="85" Level="Error"></Issue>
          |            </Messages>
          |        </Namespace>
          |    </Namespaces>
          |    <Targets>
          |        <Target>
          |            <Modules>
          |                <Module Name="rwstestxdt.dll">
          |                    <Messages>
          |                        <Issue Name="NoStrongName" Certainty="95" Level="CriticalError"></Issue>
          |                    </Messages>
          |                </Module>
          |            </Modules>
          |        </Target>
          |    </Targets>
          |</FxCopReport>'''.stripMargin()

You can do:

List<String> levels = new XmlSlurper().parseText( x )
                                      .'**'
                                      .findAll { it.name() == 'Issue' }
                                      .collect { it.@Level.text() }

To get the list:

[ 'Error', 'CriticalError' ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top