xerces-j-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Glavassevich <mrgla...@ca.ibm.com>
Subject Re: Parsing XML file using XSD validation
Date Wed, 20 Jul 2005 04:06:23 GMT
Hi Anthony,

You've enabled both schema [1] and DTD [2] validation. Since your document 
doesn't contain a DOCTYPE, I assume you didn't want to turn on DTD 
validation. You can fix this by removing 
documentBuilderFactory.setValidating(true) from your code.

Thanks.

[1] 
http://xml.apache.org/xerces2-j/javadocs/api/javax/xml/parsers/DocumentBuilderFactory.html#setSchema(javax.xml.validation.Schema)
[2] 
http://xml.apache.org/xerces2-j/javadocs/api/javax/xml/parsers/DocumentBuilderFactory.html#setValidating(boolean)

"Anthony N. Frasso" <afrasso@yahoo.com> wrote on 07/19/2005 11:15:34 PM:

> Hello, and thanks in advance for all of your help.
> 
> I am currently using Xerces (for Java) version 2.7 with the J2SE 1.4.2
> on Mac OS X version 10.3.
> 
> I am attempting to parse an XML file while validating it against an
> XSD.
> 
> Here is my code to parse the XML file:
> 
> <code>
> SchemaFactory schemaFactory =
>     SchemaFactory.newInstance(
>         XMLConstants.W3C_XML_SCHEMA_NS_URI);
> Schema schema =
>     schemaFactory.newSchema(new File("playlist.xsd"));
> System.out.println("Schema: " + schema);
> 
> DocumentBuilderFactory documentBuilderFactory =
>     DocumentBuilderFactory.newInstance();
> System.out.println("Factory: " + documentBuilderFactory);
> documentBuilderFactory.setSchema(schema);
> documentBuilderFactory.setValidating(true);
> documentBuilderFactory.setNamespaceAware(true);
> 
> DocumentBuilder documentBuilder =
>     documentBuilderFactory.newDocumentBuilder();
> System.out.println("Builder: " + documentBuilder);
> documentBuilder.setErrorHandler(new PlaylistParserErrorHandler());
> documentBuilder.parse(new File("episodes.xml"));
> </code>
> 
> playlist.xsd looks like the following:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>            targetNamespace="http://www.gafungi.com"
>            xmlns="http://www.gafungi.com"
>            elementFormDefault="qualified"
>            attributeFormDefault="qualified">
> 
>   <xs:complexType name="movieType">
>     <xs:sequence>
>       <xs:element name="uri"
>                   type="xs:anyURI"
>                   minOccurs="1"/>
>     </xs:sequence>
>   </xs:complexType>
> 
>   <xs:complexType name="playlistType">
>     <xs:sequence>
>       <xs:element name="movie"
>                   type="movieType"
>                   minOccurs="0"
>                   maxOccurs="unbounded" />
>     </xs:sequence>
>   </xs:complexType>
> 
>   <xs:element name="playlist"
>               type="playlistType" />
> 
> </xs:schema>
> 
> and episodes.xml looks like:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> <playlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>           xsi:schemaLocation="http://www.gafungi.com playlist.xsd"
>           xmlns="http://www.gafungi.com">
> 
>   <movie>
>     <uri>/Users/tony/Movies/1.avi</uri>
>   </movie>
>   <movie>
>     <uri>/Users/tony/Movies/2.avi</uri>
>   </movie>
>   <movie>
>     <uri>/Users/tony/Movies/3.avi</uri>
>   </movie>
>   <movie>
>     <uri>/Users/tony/Movies/4.avi</uri>
>   </movie>
>   <movie>
>     <uri>/Users/tony/Movies/5.avi</uri>
>   </movie>
>   <movie>
>     <uri>/Users/tony/Movies/6.avi</uri>
>   </movie>
>   <movie>
>     <uri>/Users/tony/Movies/7.avi</uri>
>   </movie>
> 
> </playlist>
> 
> When I attempt to run my code, I get the following exception:
> 
> org.xml.sax.SAXParseException: Document is invalid: no grammar found.
>         at org.apache.xerces.util.ErrorHandlerWrapper.
>            createSAXParseException(Unknown Source)
>         at org.apache.xerces.util.ErrorHandlerWrapper.
>            error(Unknown Source)
>         at org.apache.xerces.impl.XMLErrorReporter.
>            reportError(Unknown Source)
>         at org.apache.xerces.impl.XMLErrorReporter.
>            reportError(Unknown Source)
>         at org.apache.xerces.impl.XMLNSDocumentScannerImpl.
>            scanStartElement(Unknown Source)
>         at org.apache.xerces.impl.XMLNSDocumentScannerImpl
>            $NSContentDispatcher.scanRootElementHook(Unknown Source)
>         at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl
>            $FragmentContentDispatcher.dispatch(Unknown Source)
>         at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.
>            scanDocument(Unknown Source)
>         at org.apache.xerces.parsers.XML11Configuration.
>            parse(Unknown Source)
>         at org.apache.xerces.parsers.XML11Configuration.
>            parse(Unknown Source)
>         at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
>         at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
>         at org.apache.xerces.jaxp.DocumentBuilderImpl.
>            parse(Unknown Source)
>         at javax.xml.parsers.DocumentBuilder.
>            parse(Unknown Source)
> 
> Any clue what is failing here, and what the exception means?
> 
> Regards,
> Anthony Frasso
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org
> 

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Mime
View raw message