如何从w3c验证器</defect>中的Set <defect>获取一些信息

时间:2013-03-03 11:53:04

标签: w3c w3c-validation java

如何在此代码中获取一些信息(列,行,消息)?

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

但是得到例外:

  

线程中的异常&#34; main&#34; java.lang.ClassCastException:[Ljava.lang.Object;无法投射到[Lcom.rexsl.w3c.Defect;       在HTMLValidator.main(HTMLValidator.java:17)

1 个答案:

答案 0 :(得分: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());
}