0

Shouldn't something like this work?

Assuming a document formatted as such:

<root>
   <element id = "a"></element>
</root>

Node node = doc.query("/root/element").get(0);
String id = node.getDocument().getRootElement().getAttribute("id");

When I print the value of the root element, it looks as if this should work. What's failing, here?

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406

2 Answers2

2

Cast your node to an Element, and you're good to go.

Stefan Kendall
  • 66,414
  • 68
  • 253
  • 406
0

node.getDocument().getRootElement() at this point you have the element which does not have an attribute "id".

Try node.getAttribute("id") instead ? (assuming node is not null)

Kannan Ekanath
  • 16,759
  • 22
  • 75
  • 101
  • 1
    Can you try type casting Node to Element and also tell us what is failing. Also, any code pasted shown should not be considered compilable (and I hate downvoting because something does not compile). The idea is what should be looked at ? – Kannan Ekanath Mar 02 '10 at 18:28
  • When I print the root element retrieved, it shows ..., so I'm confused as to why that doesn't work. – Stefan Kendall Mar 02 '10 at 18:33