Groovy: Corrigez la syntaxe pour que XMLSlurper trouve les éléments avec un attribut donné

StackOverflow https://stackoverflow.com/questions/100325

  •  01-07-2019
  •  | 
  •  

Question

Étant donné un fichier HTML avec la structure html - > corps - > un groupe de divs quelle est la bonne déclaration groovy pour trouver tous les divs avec un attribut de balises non vierges?

Ce qui suit ne fonctionne pas:

def nodes = html.body.div.findAll { it.@tags != null }

car il trouve tous les nœuds.

Était-ce utile?

La solution

Essayez ce qui suit (Groovy 1.5.6):

def doc = """
<html>
    <body>
        <div tags="1">test1</div>
        <div>test2</div>
        <div tags="">test3</div>
        <div tags="4">test4</div>
    </body>
</html>
"""

def html = new XmlSlurper().parseText( doc)

html.body.div.findAll { it.@tags.text()}.each { div ->
    println div.text()
}

Cette sortie:

test1
test4
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top