xerces-j-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Anthony N. Frasso" <afra...@yahoo.com>
Subject Parsing XML file using XSD validation
Date Wed, 20 Jul 2005 03:15:34 GMT
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


Mime
View raw message