Java 从XML属性中解析int
我有一个节点的属性,它包含如下内容: number=“1” 我想如果我在Java 从XML属性中解析int,java,Java,我有一个节点的属性,它包含如下内容: number=“1” 我想如果我在=上解析,我就可以使用Integer.parseInt(node.getAttributes().item(i.toString()) 但这将返回以下错误: java.lang.NumberFormatException: For input string: ""1"" 所以现在我在做: String[] value = node.getAttributes().item(i).toString().split("=\""
=
上解析,我就可以使用Integer.parseInt(node.getAttributes().item(i.toString())代码>
但这将返回以下错误:
java.lang.NumberFormatException: For input string: ""1""
所以现在我在做:
String[] value = node.getAttributes().item(i).toString().split("=\"");
String[] number = value[1].split("\"");
Integer.parseInt(number[0].toString()) // contains the right value 1
有没有更好更干净的方法?我觉得这太俗气了
编辑:
节点的定义如下:
org.w3c.dom.Node Node=nodeList.item(索引) 更换
node.getAttributes().item(i).toString()
与
节点的类型/类别是什么?哦,对不起!org.w3c.dom.Node Node=nodeList.item(索引)您使用了哪个api?xml?您无法获取属性的值?解决问题的顺序:供应商文档=>Dr.Google=>StackOverflow
node.getAttributes().item(i).getNodeValue()