日期:2014-05-20 浏览次数:20785 次
/**
* 取得最后一个question的id,如果没有返回0
*/
// @SuppressWarnings("unchecked")
private static int getLastVoteId()
{
List<Element> list = XMLUtil.load(CharacterUtil.XML_PATH).getRootElement().elements();
if (0 == list.size())
{
return 0;
}
else
{
String tempIdString = null;
for (Element element : list)
{
tempIdString = element.attributeValue("id");
}
return Integer.parseInt(tempIdString);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<questions>
<question id="1" voteName="投票题目1" voteType="1">
<voteOption num="0">选项1</voteOption>
<voteOption num="0">选项2</voteOption>
<voteOption num="0">选项3</voteOption>
</question>
<question id="2" voteName="投票题目2" voteType="2">
<voteOption num="0">选项1</voteOption>
<voteOption num="0">选项2</voteOption>
</question>
</questions>
<?xml version="1.0" encoding="UTF-8"?>
<questions>
<question id="2" voteName="投票题目2" voteType="2">
<voteOption num="0">选项1</voteOption>
<voteOption num="0">选项2</voteOption>
</question>
<question id="1" voteName="投票题目1" voteType="1">
<voteOption num="0">选项1</voteOption>
<voteOption num="0">选项2</voteOption>
<voteOption num="0">选项3</voteOption>
</question>
</questions>
Node node = doc.selectSingleNode("(//question)[last()]/@id");
if(node != null) String id = node.getText();