SAX parser problem

Discussion in 'Java' started by steve_marjoribanks@hotmail.com, Feb 18, 2006.

  1. Guest

    I am writing an application which uses the following class to parse and
    validate an xml file.
    The 'filename' in the InputSource line is the file which is selected to
    be opened from the JFileChooser in another class. The problem is that
    it seems to open the file fine and it seems to find well-formedness
    errors but I don't think it is actually validating against a schema at
    all. I have tried opening an XML file which I have put validity (as
    defined by the accompanying schema) errors into and my application just
    parses it and doesn't give any errors or anything. I have set up a
    simple Handler so that I know when the parsing starts and finishes so I
    know that the file has been parsed but not validated for some reason.

    Also, when I open an XML which has an 'import' statement to import a
    schema from a remote location I get a load of runtime errors which I
    think are to do with not being able to find the file because the first
    line says 'Connection timed out' or something similar. What catch
    statement do I need to include to ensure that if the file cannot be
    found on the remotes server that it does not throw a load of
    exceptions? It does seem kinda strange though that the parser should be
    trying to find a schema if it doesn't seem to be validating against it
    anyway?!

    Thanks

    Steve


    class XMLParser
    {
    // Create control variables for namesapce awareness and validation...
    public boolean awareness = true;
    public boolean validating = true;

    public XMLParser()
    {
    try
    {
    // Create SAX 2 parser factory...
    SAXParserFactory factory = SAXParserFactory.newInstance();
    // Set parsing properties...
    factory.setNamespaceAware(awareness);
    factory.setValidating(validating);
    // Create an instance of the content handler...
    SAXHandler handler = new SAXHandler();
    // Create an instance of the SAXParser
    SAXParser saxParser = factory.newSAXParser();
    // Initialize InputSource and FileReader...
    InputSource inputSource = new
    InputSource(Application.filename.toURI().toString());
    inputSource.setSystemId(Application.filename.toURI().toString());
    // Parse the file...
    saxParser.parse(inputSource, handler);
    }
    catch (IOException fileError )
    {
    fileError.printStackTrace();
    }
    catch (SAXException SAXError)
    {
    SAXError.printStackTrace();
    }
    catch (ParserConfigurationException parserError)
    {
    parserError.printStackTrace();
    }
    }
    }
    , Feb 18, 2006
    #1
    1. Advertising

  2. wrote:
    > I am writing an application which uses the following class to parse and
    > validate an xml file.
    > The 'filename' in the InputSource line is the file which is selected to
    > be opened from the JFileChooser in another class. The problem is that
    > it seems to open the file fine and it seems to find well-formedness
    > errors but I don't think it is actually validating against a schema at
    > all.


    I am not sure but I think there may be extra properties or features one
    must set to convince the parser to use a schema to validate. Consult
    the documentation for your parser.

    HTH,
    Ray
    --
    This signature intentionally left blank.
    Raymond DeCampo, Feb 18, 2006
    #2
    1. Advertising

  3. Guest

    Ahh, found the problem now, I was mixing up using SAX as part of JAXP
    and SAX by itself. And yes there are other properties I need to set,
    and I need to use an ErrorHandler as well.
    , Feb 19, 2006
    #3
  4. Guest

    Ok, having gone away and looked at my problem further, I have now
    ammended my original code so I am now using JAXP, but I'm still
    struggling, more due to me misunderstanding the documentation I think
    as opposed to actual errors in the code. The small section of code
    dealing with the parsing is at the bottom of this post.

    I am struggling to understand the process of validating an XML document
    against a given schema. Currently, I am using SAXParserFactory and
    turning validation on by using factory.setValidation(true);

    However, from what I can understand from the documentation this just
    turns on validation, whether it be against a DTD or any other schema
    language. In my application I wish to be able to open an XML file and
    it against a W3C Schema as specified by the 'xs:schemaLocation'
    attribute within the XML file, which will probably be in a remote
    location. From what I have read, it is possible to set the validation
    language to W3C Schema but this is only possible by using the
    alternative validation method of using properties and features to set
    the parser up and not by simply using the setValidation() method. What
    is the difference between these methods in terms of validation?
    Therefore, my question is what is the best way to make my application
    validate the XML document against the W3C schema as given in the XML
    file, and how should I go about it? Also, if anyone knows of any
    tutorials or similar covering this subject area and could point me in
    their direction, I would be most grateful.

    Many thanks

    Steve

    // Create class to parse XML file using JAXP...
    class XMLParser
    {
    // Create control variables for namespace awareness and validation...
    public boolean nsAwareness = true;
    public boolean validating = true;

    public XMLParser()
    {
    try
    {
    // Create JAXP SAX parser factory...
    SAXParserFactory factory = SAXParserFactory.newInstance();
    // Set parsing properties...
    factory.setNamespaceAware(nsAwareness);
    factory.setValidating(validating);
    // Create an instance of the content handler and error handler...
    SAXHandler handler = new SAXHandler();
    XMLErrorHandler errorHandler = new XMLErrorHandler();
    // Create an instance of the SAXParser and set handlers...
    XMLReader xmlReader = factory.newSAXParser().getXMLReader();
    xmlReader.setContentHandler(handler);
    xmlReader.setErrorHandler(errorHandler);
    // Initialize InputSource...
    InputSource inputSource = new
    InputSource(Application.filename.toURI().toString());
    inputSource.setSystemId(Application.filename.toURI().toString());
    // Parse the file...
    xmlReader.parse(inputSource);
    }
    catch (IOException fileError )
    {
    fileError.printStackTrace();
    }
    catch (SAXException SAXError)
    {
    SAXError.printStackTrace();
    }
    catch (ParserConfigurationException parserError)
    {
    parserError.printStackTrace();
    }
    }
    }
    , Feb 19, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin Schlatter

    Encoding problem with SAX parser

    Martin Schlatter, Dec 10, 2003, in forum: Java
    Replies:
    2
    Views:
    842
    Martin Schlatter
    Dec 14, 2003
  2. Mladen Adamovic
    Replies:
    0
    Views:
    722
    Mladen Adamovic
    Jan 14, 2005
  3. Replies:
    5
    Views:
    16,211
    Steve W. Jackson
    Sep 15, 2005
  4. Replies:
    0
    Views:
    889
  5. Mize-ze

    SAX Parser problem

    Mize-ze, Nov 13, 2006, in forum: Java
    Replies:
    12
    Views:
    1,048
    Chris Uppal
    Nov 20, 2006
Loading...

Share This Page