Stefan Kendall
Stefan Kendall

Reputation: 67832

XOM getting attribute from Node?

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?

Upvotes: 0

Views: 1661

Answers (2)

Stefan Kendall
Stefan Kendall

Reputation: 67832

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

Upvotes: 2

Kannan Ekanath
Kannan Ekanath

Reputation: 17601

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)

Upvotes: 0

Related Questions