2013-03-03 32 views
2

如何获得一些infromation我怎么能得到一些信息(列,行,消息)在此代码?如何从设置在W3C验证器

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

但是得到的异常:在线程 “主要” java.lang.ClassCastException

例外:[Ljava.lang.Object;不能投射到[Lcom.rexsl.w3c.Defect; 在HTMLValidator.main(HTMLValidator.java:17)

+0

可能重复(http://stackoverflow.com/questions/380813/downcasting-in-java) – 2013-10-18 00:38:24

回答

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()); 
} 
的[向下转换在Java中]