以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  [原创]使用dom4j1.6.1对xml进行Schema验证  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=71026)


--  作者:libaiyu
--  发布时间:12/30/2008 3:15:00 PM

--  [原创]使用dom4j1.6.1对xml进行Schema验证
请教使用dom4j1.6.1对xml进行xsd验证:
在xml中设xsi:schemaLocation="http://www.w3schools.com note.xsd时能验证通过,但若去掉通过代码设计
String url = getClass().getResource("note.xsd").toString();
reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", url);就通不过,总找不到
vc-elt.1: Cannot find the declaration of element 'note'
我怀疑是不是找不到xsd文件?但是我已经把note.xsd拷贝该类下了。
并且换为其他的方式也还是不行,比如:reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",  
                                                 "f:\\test\note.xsd");
该源码拷贝来自网上例子:
SAXReader reader = new SAXReader();  
       
       reader.setValidation(true);  
             try {  
              reader.setFeature("http://xml.org/sax/features/validation", true);  
              reader.setFeature("http://apache.org/xml/features/validation/schema", true);  
             reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
               reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",  
                                                 "f:\\test\note.xsd");  
    
                XMLErrorHandler errorHandler = new XMLErrorHandler();  
               reader.setErrorHandler(errorHandler);  
  
                reader.read("f:\\note.xml");  
                XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());  
                if (errorHandler.getErrors().hasContent()) {  
                       writer.write(errorHandler.getErrors());  
                } else {  
                       System.out.println("validate success.");  
                }  
             } catch (SAXException e) {  
                e.printStackTrace();  
           } catch (DocumentException e) {  
               e.printStackTrace();  
            } catch (UnsupportedEncodingException e) {  
                e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }
xml和xsd文件在附件中


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