You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Thomas Bierhance <uf...@stud.uni-karlsruhe.de> on 2001/12/14 19:18:07 UTC

Schema validating XML parsing

Hello xerces-j-user,

  I just started using Xerces (1.4.4) and have a problem parsing a XML
  that is assigned to an XML Schema. I had a look on the SAXCounter
  example, that succeeds in validating my Files (XML Spy does as
  well), but it is using deprecated API. So I tried to do it a
  different way...

    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setValidating(true);
    
    File f = new File("E:/Projekte/sampleDoku.xml");
    
    try {
      SAXParser parser = parserFactory.newSAXParser();
      parser.parse(f, new MyHandler());
    } catch (Exception e) {
      e.printStackTrace();
    }

  "MyHandler" extends "DefaultHandler" and gives error msg like...

    [Warning] Valid documents must have a <!DOCTYPE declaration.
    [Error] Element type "moduledescription" is not declared.
    [Error] Attribute "xmlns:xsi" is not declared for element "moduledescription".
    [Error] Attribute "xsi:noNamespaceSchemaLocation" is not declared for element "moduledescription".

  That looks to me like my schema is not found.

  What is going on here, where is my fault? What is the official (not
  deprecated) way to do this using the JAXP interfaces?
  
--
Regards and Thanks,

 Thomas                          mailto:uf4b@stud.uni-karlsruhe.de


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


Re: Schema validating XML parsing

Posted by Thomas Bierhance <uf...@stud.uni-karlsruhe.de>.
Hello,

  thanks a lot for you answer. After my JAXP conform attempt failed, I
  coded something very similar to your code.
  Only problem is - if I understand everything right, I'm just
  beginnning - that this code is specific to Xerces and won't work
  with other JAXP implementing parsers.
  I'll try and give Torsten's approach a go.
  
--
Regards,
 Thomas                            mailto:uf4b@stud.uni-karlsruhe.de


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


Re[2]: Schema validating XML parsing

Posted by Thomas Bierhance <uf...@stud.uni-karlsruhe.de>.
Hallo Torsten,

TC> I remeber I had some problems with
TC> "noNamespaceSchemaLocation", too.
  No problems with that here.

TC>       spf.setNamespaceAware(true);
  THAT'S IT! Everythings works now. Thanks a lot! Would have never got
  this on my own....
  
-- 
Gruß,
 Thomas                            mailto:uf4b@stud.uni-karlsruhe.de


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


Re: Schema validating XML parsing

Posted by Torsten Curdt <tc...@dff.st>.
IMHO your JAXP way of getting the parser looks fine.
But I'm quite sure you also have to enable
namespaces. Looking at your error messages
I remeber I had some problems with
"noNamespaceSchemaLocation", too.
Can't remember exactly what is was - anyway we now
use "xsi:schemaLocation".

Here is a small sample that works for me:

      SAXParserFactory spf = SAXParserFactory.newInstance();
      spf.setValidating(true);
      spf.setNamespaceAware(true);
      XMLReader parser = spf.newSAXParser().getXMLReader();

      parser.setContentHandler(this);
      parser.setErrorHandler(this);

      InputSource source = new InputSource(reader);

      parser.parse(source);

Hope this helps
--
Torsten

On Mon, 17 Dec 2001, Arturo Ma wrote:
> Try this:
>
> private static final String PARSER_NAME =
> "org.apache.xerces.parsers.SAXParser";
>
> private static boolean setValidation    = true;
> private static boolean setNameSpaces    = true;
> private static boolean setLoadExternalDTD = true;
> private static boolean setSchemaSupport = true;
> private static boolean setSchemaFullSupport = false;
>
>  XMLReader xr;
>
>   try {
>    // Create the XML reader...
>    xr = XMLReaderFactory.createXMLReader(PARSER_NAME);
>   }
>   catch (Exception e) {
>       // Handle the exception
>   }
>
>        // Set Parser features
>         try {
>               // Activate/Deactivate  Parser Validation
>               xr.setFeature("http://xml.org/sax/features/validation",
> setValidation);
>
>              // Activate/Deactivate  Namespace Support
>             xr.setFeature("http://xml.org/sax/features/namespaces",
> setNameSpaces);
>
>             // Activate/Deactivate  XML Schema support
>
> xr.setFeature("http://apache.org/xml/features/validation/schema",
> setSchemaSupport);
>
>             // Activate/Deactivate Schema Full Support
>
> xr.setFeature("http://apache.org/xml/features/validation/schema-full-checkin
> g", setSchemaFullSupport);
>         }
>         catch (SAXException e) {
>             // Handle the exception
>         }
>
>         try {
>           // Set the ContentHandler...
>           xr.setContentHandler("your handler");
>           xr.setErrorHandler("your handler");
>
>         xr.parse( XML );
>         }
>         catch (SAXException e) {
>             // Handle the exception
>         }
>
>
> Hope this helps...
>
> Arturo Ma
> -------------------------------------------------------------
> Arturo Ma
> ama@fgm.com
> FGM Inc.
> 2820 Camino Del Rio South, Suite 130
> San Diego, CA 92108
> Office: 619. 297.2905
> Fax: 619. 297.2923
> http://www.fgm.com
>
>
> ----- Original Message -----
> From: "Thomas Bierhance" <uf...@stud.uni-karlsruhe.de>
> To: <xe...@xml.apache.org>
> Sent: Friday, December 14, 2001 10:18 AM
> Subject: Schema validating XML parsing
>
>
> > Hello xerces-j-user,
> >
> >   I just started using Xerces (1.4.4) and have a problem parsing a XML
> >   that is assigned to an XML Schema. I had a look on the SAXCounter
> >   example, that succeeds in validating my Files (XML Spy does as
> >   well), but it is using deprecated API. So I tried to do it a
> >   different way...
> >
> >     SAXParserFactory parserFactory = SAXParserFactory.newInstance();
> >     parserFactory.setValidating(true);
> >
> >     File f = new File("E:/Projekte/sampleDoku.xml");
> >
> >     try {
> >       SAXParser parser = parserFactory.newSAXParser();
> >       parser.parse(f, new MyHandler());
> >     } catch (Exception e) {
> >       e.printStackTrace();
> >     }
> >
> >   "MyHandler" extends "DefaultHandler" and gives error msg like...
> >
> >     [Warning] Valid documents must have a <!DOCTYPE declaration.
> >     [Error] Element type "moduledescription" is not declared.
> >     [Error] Attribute "xmlns:xsi" is not declared for element
> "moduledescription".
> >     [Error] Attribute "xsi:noNamespaceSchemaLocation" is not declared for
> element "moduledescription".
> >
> >   That looks to me like my schema is not found.
> >
> >   What is going on here, where is my fault? What is the official (not
> >   deprecated) way to do this using the JAXP interfaces?
> >
> > --
> > Regards and Thanks,
> >
> >  Thomas                          mailto:uf4b@stud.uni-karlsruhe.de
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-j-user-help@xml.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>
>


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


Re: Schema validating XML parsing

Posted by Arturo Ma <am...@fgm.com>.
Try this:

private static final String PARSER_NAME =
"org.apache.xerces.parsers.SAXParser";

private static boolean setValidation    = true;
private static boolean setNameSpaces    = true;
private static boolean setLoadExternalDTD = true;
private static boolean setSchemaSupport = true;
private static boolean setSchemaFullSupport = false;

 XMLReader xr;

  try {
   // Create the XML reader...
   xr = XMLReaderFactory.createXMLReader(PARSER_NAME);
  }
  catch (Exception e) {
      // Handle the exception
  }

       // Set Parser features
        try {
              // Activate/Deactivate  Parser Validation
              xr.setFeature("http://xml.org/sax/features/validation",
setValidation);

             // Activate/Deactivate  Namespace Support
            xr.setFeature("http://xml.org/sax/features/namespaces",
setNameSpaces);

            // Activate/Deactivate  XML Schema support

xr.setFeature("http://apache.org/xml/features/validation/schema",
setSchemaSupport);

            // Activate/Deactivate Schema Full Support

xr.setFeature("http://apache.org/xml/features/validation/schema-full-checkin
g", setSchemaFullSupport);
        }
        catch (SAXException e) {
            // Handle the exception
        }

        try {
          // Set the ContentHandler...
          xr.setContentHandler("your handler");
          xr.setErrorHandler("your handler");

        xr.parse( XML );
        }
        catch (SAXException e) {
            // Handle the exception
        }


Hope this helps...

Arturo Ma
-------------------------------------------------------------
Arturo Ma
ama@fgm.com
FGM Inc.
2820 Camino Del Rio South, Suite 130
San Diego, CA 92108
Office: 619. 297.2905
Fax: 619. 297.2923
http://www.fgm.com


----- Original Message -----
From: "Thomas Bierhance" <uf...@stud.uni-karlsruhe.de>
To: <xe...@xml.apache.org>
Sent: Friday, December 14, 2001 10:18 AM
Subject: Schema validating XML parsing


> Hello xerces-j-user,
>
>   I just started using Xerces (1.4.4) and have a problem parsing a XML
>   that is assigned to an XML Schema. I had a look on the SAXCounter
>   example, that succeeds in validating my Files (XML Spy does as
>   well), but it is using deprecated API. So I tried to do it a
>   different way...
>
>     SAXParserFactory parserFactory = SAXParserFactory.newInstance();
>     parserFactory.setValidating(true);
>
>     File f = new File("E:/Projekte/sampleDoku.xml");
>
>     try {
>       SAXParser parser = parserFactory.newSAXParser();
>       parser.parse(f, new MyHandler());
>     } catch (Exception e) {
>       e.printStackTrace();
>     }
>
>   "MyHandler" extends "DefaultHandler" and gives error msg like...
>
>     [Warning] Valid documents must have a <!DOCTYPE declaration.
>     [Error] Element type "moduledescription" is not declared.
>     [Error] Attribute "xmlns:xsi" is not declared for element
"moduledescription".
>     [Error] Attribute "xsi:noNamespaceSchemaLocation" is not declared for
element "moduledescription".
>
>   That looks to me like my schema is not found.
>
>   What is going on here, where is my fault? What is the official (not
>   deprecated) way to do this using the JAXP interfaces?
>
> --
> Regards and Thanks,
>
>  Thomas                          mailto:uf4b@stud.uni-karlsruhe.de
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
>


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