我试图将字符串值解组为整数,它返回int的默认值而不是抛出异常。是否有任何解决方案来处理异常。 因为我需要原始值[aa]用于记录目的。
public class TestJAXB {
public static void main (String a[]) {
String xml = "<Message><age>aa</age><name>shas</name></Message>";
try {
JAXBContext j = JAXBContext.newInstance(TestBean.class);
Unmarshaller u = j.createUnmarshaller();
TestBean o = (TestBean) u.unmarshal(new StringReader(xml));
System.out.println("Age "+o.getAge()); // returns 0 instead of throwing exception
} catch (Exception e) {
e.printStackTrace();
}
}
}
class TestBean {
@XmlElement
private int age = 0;
@XmlElement
private String name = null;
}