4

How do I select from a parsed html document a specific element given its index.

For example: ...

<div>div1</div>
<div>div2</div>

I want to select the second div but it seems to me GPath doesn't offer a solution like Xpath does.

lospejos
  • 1,976
  • 3
  • 19
  • 35
SomeEUGuy
  • 187
  • 1
  • 1
  • 7

1 Answers1

7
def html = """
<html>
  <head>
    <title>test</title>
  </head>
  <body>
    <div>div1</div>
    <div>div2</div>
  </body>
</html>"""

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

assert xml.body.div[0].text() == "div1"
assert xml.body.div[1].text() == "div2"

You can also use collection type methods on the div node such as .each/.find, for example:

xml.body.div.find { it.text() == "div2" }

EDIT:

To clarify my answer a bit, given HTML in the same structure as the sample I listed above but with various content, you can always access the second div using array index 1:

xml.body.div[1]
John Wagenleitner
  • 10,967
  • 1
  • 40
  • 39
  • i don't know the content of the divs but i know that the info that is important for me is in the second one ... – SomeEUGuy Jan 06 '11 at 06:20
  • I edited my answer to clarify it a bit. The asserts were just there to show which content it accessed. Using div[1] should get you the second div node. – John Wagenleitner Jan 06 '11 at 10:40