2

How I can get some information (column, line, message) at this code?

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());
}

I tried to write as:

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());
 }

But get exception:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lcom.rexsl.w3c.Defect; at HTMLValidator.main(HTMLValidator.java:17)

4

1 回答 1

0

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());
}
于 2014-01-04T17:53:38.997 回答