0

You need to read the xml and display only those values where there are INNYL or INNFL tags. But if I do not have any of them in the "Document" or it is empty, then the output is: INNYL INNFL Why is that? I need nothing to be displayed in this case, since there are no values (there is no tag or it is empty).

 def person = new XmlSlurper().parse(new File("C:\\test\\test.xml"))

        person.Document.findAll { p ->
            p.Org.@INNYL != null ||
            p.IPV.@INNFL != null
        }.each { p ->
            println "INNYL ${p.Org.@INNYL} INNFL ${p.IPV.@INNFL}"
        }

I didn't attach XML, as you can create any.

tim_yates
  • 167,322
  • 27
  • 342
  • 338
Ekz0
  • 63
  • 1
  • 8

1 Answers1

1

Because @INNYL returns an empty attribute, not null... You can check it by getting the text, and check if they're empty

    person.Document.findAll { p ->
        p.Org.@INNYL.text() ||
        p.IPV.@INNFL.text()
    }.each { p ->
        println "INNYL ${p.Org.@INNYL} INNFL ${p.IPV.@INNFL}"
    }

Or you can use isEmpty() , ie: !p.Org.@INNYL.isEmpty()

tim_yates
  • 167,322
  • 27
  • 342
  • 338