以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  关于schema验证不合法的xml的出错信息定位问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=42981)


--  作者:hattah
--  发布时间:2/2/2007 1:19:00 PM

--  关于schema验证不合法的xml的出错信息定位问题

代码是从论坛上改出来的,如下:

builder.setFeature("http://apache.org/xml/features/validation/schema",true);  

builder.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","file:////c://test.xsd");
builder.setErrorHandler(
               new org.xml.sax.ErrorHandler() {
                   public void fatalError(SAXParseException spe)
                           throws SAXException {
                       System.out.println("** Warning1"
                           + ", line " + spe.getLineNumber()
                           + ", uri " + spe.getSystemId());
                       System.out.println("   " + spe.getMessage());

                       throw spe;
                   }
                   // treat validation errors as fatal
                   public void error(SAXParseException spe)
                           throws SAXParseException {
                       System.out.println("** Warning2"
                           + ", line " + spe.getLineNumber()
                           + ", uri " + spe.getSystemId());
                       spe.printStackTrace();
                       System.out.println("   " + spe.getMessage());

                       throw spe;
                   }

                    // dump warnings too
                   public void warning(SAXParseException err)
                           throws SAXParseException {
                     System.out.println("** Warning3"
                       + ", line " + err.getLineNumber()
                       + ", uri " + err.getSystemId());
                     System.out.println("   " + err.getMessage());
                   }              
           });  

    builder.build(new FileReader(xml));

但是这样检验一个不合法的xml时,报错的信息很难定位,比如:
** Warning2, line 27, uri null
   cvc-datatype-valid.1.2.1: '' is not a valid 'integer' value.
org.jdom.input.JDOMParseException: Error on line 27: cvc-datatype-valid.1.2.1: '' is not a valid 'integer' value.
 at org.jdom.input.SAXBuilder.build(SAXBuilder.java:468)
 at org.jdom.input.SAXBuilder.build(SAXBuilder.java:851)
 at com.founder.mp.context.import4fit.TestXsd.validate(TestXsd.java:69)
 at com.founder.mp.context.import4fit.TestXsd.main(TestXsd.java:18)

////////////////////////////////////////////////////////////////////////////
能不能在xsd中事先配置好出错时该显示的信息,比如:
<xs:annotation>a must be string</xs:annotation>
<xs:element name="a" type="xs:string"/>这样,一但验证出错则打出“a must be string”提示?

请问各位有什么好方法么?

bow


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
2,980.469ms