Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem validating schema

 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys I really need ur help here..
I am trying to validate a schema file using JAXP1.2.. Here is my code .. I have no clue why I get this error
Schema_reference 4: Failed to read schema document 'null'

//constants
static String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";

try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
SAXParser saxParser = spf.newSAXParser();
try
{
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
}
catch (SAXNotRecognizedException x)
{
throw new RuntimeException( "Parser doesn't support XML Schema. "+x.getMessage());
}
saxParser.setProperty(JAXP_SCHEMA_SOURCE, new File("c:\\projects\\xml Mssaging\\bin\\XMLSchema.xsd"));
// Validate
XMLReader xmlReader = saxParser.getXMLReader();
errorChecker = new ErrorChecker( verboseErrors );
xmlReader.setErrorHandler( errorChecker );

// Validate
xmlReader.parse(new InputSource(new FileReader(inputFile)));

// If error happened
if ( errorChecker.errorHappened )
{
isValid = false;
// Suggest "verboseerrors" att if not on
if ( ! verboseErrors )
{
System.out.println( "Error. Set SchemaValidate's 'verbose errors' attribute to true for a more detailed description on warnings.");
}
}
}
catch (IOException e)
{
isValid = false;
System.err.println("Error: IO Exception.");
e.printStackTrace();
}
catch (SAXException e)
{
isValid = false;
System.err.println("Error: SAXException.");
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
isValid = false;
System.err.println("Error: ParserConfigurationException.");
e.printStackTrace();
}

I would really appreciate any help...This thing has been bugging me for a few days now..
Thanks,
Roopa
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roopa,
I tried your code wich works perfectly for me. The thing that might not be working is the location of the xsd file (this is actually the error reported by your code: failed to read schema document).
All your have to do is check the location of the file. The path "c:\\projects\\xml Mssaging\\bin\\XMLSchema.xsd" might actually be wrong (don't you miss a e in Mssaging for instance?).
Cheers
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reference, I used the following files for testing:
- roopa.xml
- roopa.xsd
- Roopa.java
Contents of roopa.xml:

Contents of roopa.xsd:

Contents of Roopa.java:

Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic