Strus2 learning Exception Handling highlights (1), strus2exception
2015-01-05
A Method written by dom4j used to change the node text reported the following error:
Code:
for(Iterator itr=root.elementIterator();itr.hasNext();){ Element ele=(Element)itr.next(); System.out.println((Element)ele.element("name")==null); if(ele.element("name").getText().equals(name)){ ele.element("age").setText(age); break; } }
Xml:
<? Xml version = "1.0" encoding = "gb2312"?> <Student> <name> Zhang San </name> <age> 20 </age> <name> Li Si </name> <age> 30 </age> </Student>
Analysis:
Let's review his code. First, exception will solve some problems.
Catch (IOException e ){
E. printStackTrace ();
}
Catch (incluentexception e ){
E. printStackTrace ();
}
So that the key to the error cannot be seen later!
The code snippet is used to search for the "name" element under the subnode loop of "student". In this case, it becomes "name" under "name", so the node cannot be found. If the exception is normal, the reminder element cannot be found.
Remove the for loop and the code runs normally to get the desired result!