以文本方式查看主题

-  中文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=14987)


--  作者:FlashFish82
--  发布时间:3/3/2005 12:28:00 AM

--  一段验证schema程序  可是验证xml
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;
/*
* Created on 2005-3-2
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/

/**
* @author YuJieHua
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/

public class XSDTest implements ErrorHandler
{
 public static void main(String[] args)
 {
  String xmlTestPath = "f:\\eclipse\\workspace\\test.xml";
  DocumentBuilderFactory docBuilderFactory = null;
  DocumentBuilder docBuilder = null;
  org.apache.xerces.parsers.DOMParser parsers =
   new org.apache.xerces.parsers.DOMParser();
     
      try {  
        parsers.setFeature("http://xml.org/sax/features/validation",true);
         } catch (SAXNotRecognizedException e) {
            System.out.print("Unrecognized feature: ");  
            System.out.println("http://xml.org/sax/features/validation");  
         } catch (SAXNotSupportedException e) {  
            System.out.print("Unrecognized feature: ");  
            System.out.println("http://xml.org/sax/features/validation");
         }  

  try
  {
   docBuilderFactory = DocumentBuilderFactory.newInstance();
   docBuilderFactory.setValidating(true);
   docBuilderFactory.setIgnoringElementContentWhitespace(true);
   docBuilderFactory.setCoalescing(true);
   docBuilderFactory.setIgnoringComments(true);
   docBuilderFactory.setExpandEntityReferences(true);
   docBuilderFactory.setNamespaceAware(false);//false);//true);
   docBuilder = docBuilderFactory.newDocumentBuilder();
   docBuilder.setErrorHandler(new XSDTest());
   Document doc = docBuilder.parse(xmlTestPath);
  }
  catch(Exception se)
  {
   se.printStackTrace();
  }
 }
 
 public void error(SAXParseException se)
 {
  System.out.println("Caught an error:" + se.getMessage());
  //se.printStackTrace();
 }
 
 public void fatalError(SAXParseException se)
 {
  System.out.println("Caught an fatal error.");
  se.printStackTrace();
 }
 
 public void warning(SAXParseException se)
 {
  System.out.println("Caught a warning.");
  se.printStackTrace();
 }
}

出错为:Caught an error:Document root element "Patents", must match DOCTYPE root "null".
Caught an error:Document is invalid: no grammar found.
我使用的xml与xsd文件是利用xmlspy生成的 无论什么样都出错!

急问!!!!!


--  作者:树妖
--  发布时间:3/14/2005 4:30:00 PM

--  
应该这样写:
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setFeature("http://apache.org/xml/features/validation/schema",true);
你漏了后面一句。

后面一句的写法应该和解析器有关吧,我用xerces测试是通过的,如果还是不行的话,你就查一下你使用的解析器提供的Feature,应该有一个是用来启动schema处理的,你只启动了校验,没有启动schema处理,当然会出错了

这个问题放了好几天了,我天天来看,我也想知道阿,为什么没人回答呢?虽然很简单
不要紧,我自己查资料,测试,搞了几天,我搞懂了,我来告诉你,说真的,这里学习气氛不浓哦!
呼吁互相帮助!!!!!!


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