如何在这段代码中获得一些信息(列、行、消息)?
String xhtml = "<html><body><p>Hello, world!<p></body></html>";
ValidationResponse response = new ValidatorBuilder().html().validate(xhtml);
if (!response.valid())
{
Set<Defect> errors = response.errors();
//... what write at this place?
System.out.println(errors[0].column() + " " + errors[0].source());
}我试着写成:
String xhtml = "<html><body><p>Hello, world!<p></body></html>";
ValidationResponse response = new ValidatorBuilder().html().validate(xhtml);
if (!response.valid())
{
Set<Defect> errors = response.errors();
Defect[] errorsArray = (Defect[]) errors.toArray();
System.out.println(errorsArray[0].column() + " " + errorsArray[0].source());
}但要破例:
线程"main“java.lang.ClassCastException中的异常:[Ljava.lang.Object;不能转换为[Lcom.rexsl.w3c.Defect;at HTMLValidator.main(HTMLValidator.java:17) ]
发布于 2014-01-04 17:53:38
toArray()返回一个Object[]。如果您想要一个Defect[],您应该使用重载版本:
String xhtml = "<html><body><p>Hello, world!<p></body></html>";
ValidationResponse response = new ValidatorBuilder().html().validate(xhtml);
if (!response.valid())
{
Set<Defect> errors = response.errors();
Defect[] errorsArray = errors.toArray(new Defect[errors.size()]);
System.out.println(errorsArray[0].column() + " " + errorsArray[0].source());
}https://stackoverflow.com/questions/15185358
复制相似问题