无法用 groovy 解析 xml

unable to parse xml with groovy

我无法读取 xml 和解析项目,因为它总是返回

错误

groovy.lang.MissingPropertyException: No such property: method for class: Script1

def xmlText = new XmlSlurper().parse('myxml.xml')
def skipped = 0
def failed = 0
def total =  0
def passed = 0
xmlText.suite.test.class.test-method.each{ 
   if(it['@is-config'] == "true"){
   }
   else{
      if(it['@status']=="PASS"){
         passed  = passed + 1
         total = total + 1
      }
      if(it['@status']=="FAIL"){
         failed = failed + 1
         total = total + 1
     }
     if(it['@status']=="SKIP"){
         skipped = skipped + 1
         total = total + 1
     }
  }
}

xml 文件是

<?xml version="1.0" encoding="UTF-8"?>
<testng-results skipped="3" failed="0" total="3" passed="0">
  <reporter-output>
  </reporter-output>
  <suite name="DinamicTestSuite" duration-ms="24" started-at="2018-07-16T13:26:48Z" finished-at="2018-07-16T13:26:48Z">
    <groups>
    </groups>
    <test name="Device:null" duration-ms="24" started-at="2018-07-16T13:26:48Z" finished-at="2018-07-16T13:26:48Z">
      <class name="com.automation.venues.stg.VenuesSuite">
        <test-method status="PASS" signature="login(org.testng.ITestContext, java.lang.String)[pri:0, instance:com.automation.venues.stg.VenuesSuite@71454b9d]" name="login" is-config="true" duration-ms="11" started-at="2018-07-16T16:26:48Z" finished-at="2018-07-16T16:26:48Z">
</test-method>
             <test-method status="PASS" signature="beforeMethod(java.lang.reflect.Method)[pri:0, instance:com.automation.venues.stg.VenuesSuite@71454b9d]" duration-ms="0" started-at="2018-07-16T16:26:48Z" finished-at="2018-07-16T16:26:48Z">
</test-method>
      </class> <!-- com.automation.venues.stg.VenuesSuite -->
    </test> <!-- Device:null -->
  </suite> <!-- DinamicTestSuite -->
</testng-results>

如何解析列表?

您需要在带有 - 的属性周围加上引号

xmlText.suite.test.class.'test-method'.each{ 

您的 each 方法的替代方法是:

def xmlText = new XmlSlurper().parse('myxml.xml')

def totals = xmlText.suite.test.class.'test-method'.countBy { it.@status.text() }

def skipped = totals.SKIP
def failed = totals.FAIL
def passed = totals.PASS
def total = totals.values().sum()