BigBug
BigBug

Reputation: 6290

Parsing Ints out of XML attributes

so i have an attribute to a node which contains something like this: number="1"

i thought if i parse on = I could just use Integer.parseInt(node.getAttributes().item(i).toString()));

but this returns the following error:

java.lang.NumberFormatException: For input string: ""1""

so now i'm doing:

String[] value = node.getAttributes().item(i).toString().split("=\"");
String[] number = value[1].split("\"");
Integer.parseInt(number[0].toString()) // contains the right value 1

is there a better, cleaner way of doing this? feel like this is cheesy..

EDIT:

node is defined this way: org.w3c.dom.Node node = nodeList.item(index);

Upvotes: 2

Views: 85

Answers (1)

Matt Ball
Matt Ball

Reputation: 359876

Replace

node.getAttributes().item(i).toString()

with

node.getAttributes().item(i).getNodeValue()

http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html#getNodeValue%28%29

Upvotes: 9

Related Questions