-- 作者: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生成的 无论什么样都出错! 急问!!!!!
|