DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page problem in SAX XSD schema validator
Notices
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Jan 2009
Lightbulb problem in SAX XSD schema validator - 01-31-2009, 04:31 PM

hi
I've tried this code to validate XML using xsd file:
Code:
 
public static void validate(String xmlFilePath, URL schemaFileUrl)
	{
		try
		{
			SAXParserFactory factory = SAXParserFactory.newInstance();
			factory.setValidating(false);// stop standard validation to allow
											// custom schema
			factory.setNamespaceAware(true);

			SchemaFactory schemaFactory = SchemaFactory
					.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);

			factory.setSchema(schemaFactory.newSchema(schemaFileUrl));
			SAXParser parser = factory.newSAXParser();

			XMLReader reader = parser.getXMLReader();

			// Register the error handler
			reader.setErrorHandler(new JS_ValidationError());
			// Parse the file as the first argument on the command-line
			reader.parse(xmlFilePath);
		} 
		catch (SAXException e)
		{
			System.out.println("Error: " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e)
		{
			System.out.println("Error: " + e.getMessage());
			e.printStackTrace();
		} catch (ParserConfigurationException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
I have called this method ,but it issues this error :"unknown protocol" followed by windows partition name, in the debug I found that the value of the url is "file://(windows-partiton-char)/rest of path" !!!
this should work but it doesn't!!

so how can I fix this problem?

thanks
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 12
Thanks: 3
Thanked 1 Time in 1 Post
Join Date: Jan 2009
Default 02-02-2009, 06:10 AM

just as a suggestion and you might have already thought of this, but try creating a file input stream to pass to the reader.parse() method in stead of passing the file path as a string.

Another suggestion. I use JAXB for my metadata entry applications, it works very well, but it might be over kill for what you're trying to do. https://jaxb.dev.java.net/
Reply With Quote
The Following User Says Thank You to pjupson For This Useful Post:
waleedcs2000 (02-02-2009)
  (#3 (permalink)) Old
Member
 
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Join Date: Jan 2009
Default 02-02-2009, 10:52 AM

hi
some friend sent me this code , and it works by using FileInputStream, I hope this Schema class can be fixed in next java releases!!

Code:
package schemaValidationPackage;

/*
 * XsdSchemaSaxValidator.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */



import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Schema;
import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.sax.SAXSource;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import javax.xml.validation.Validator;
import java.io.*;
import java.net.URL;

//also thanks for developer Hind Abd-El-Khalek
public class XsdSchemaSaxValidator {
 
	public static void validate(String xmlFilePath, URL schemaFileUrl)
    {
        try
        {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(false);// stop standard validation to allow
                                            // custom schema
            factory.setNamespaceAware(true);
 
            SchemaFactory schemaFactory = SchemaFactory
                    .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
 
            factory.setSchema(schemaFactory.newSchema(schemaFileUrl));
            SAXParser parser = factory.newSAXParser();
 
            XMLReader reader = parser.getXMLReader();
 
            // Register the error handler
            reader.setErrorHandler(new JS_ValidationError());
            // Parse the file as the first argument on the command-line
            reader.parse(xmlFilePath);
        } 
        catch (SAXException e)
        {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e)
        {
            System.out.println("Error: " + e.getMessage());
            e.printStackTrace();
        } catch (ParserConfigurationException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

  public static void validateXml(Schema schema, String xmlName) {
    try {
      // creating a Validator instance
      Validator validator = schema.newValidator();
     
      // preparing the XML file as a SAX source
      SAXSource source = new SAXSource(
        new InputSource(new java.io.FileInputStream(xmlName)));

      // validating the SAX source against the schema
      validator.validate(source);
      System.out.println();
      System.out.println("Validation passed.");

    } catch (Exception e) {
      // catching all validation exceptions
      System.out.println();
      System.out.println(e.toString());
    }
  }
  public static Schema loadSchema(String name) {
    Schema schema = null;
    try {
      String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
      SchemaFactory factory = SchemaFactory.newInstance(language);
      schema = factory.newSchema(new File(name));
    } catch (Exception e) {
      System.out.println(e.toString());
    }
    return schema;
  }
}
thanks pjupson for your concern and help.
Reply With Quote
Reply

Tags
java, schema, validate, xml, xsd

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XHTML Validator problem ... MrGonzalez Eclipse 2 12-22-2008 01:02 AM
XML file from XSD schema ramlok Java 1 06-24-2008 07:22 AM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com