문제

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.

도움이 되었습니까?

해결책

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' ]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top