Women in Technology

Hear us Roar



Article:
  XML Document Validation with an XML Schema
Subject:   Error while validating with a DTD.
Date:   2006-08-03 13:51:13
From:   Salli
Hi,
I am trying to follow your code and validate my xml file against a DTD, specified externally (as my xml file does not come with a DOCTYPE element). Please see the code below.


It gives me the following error:

XML Document has Error:true cvc-elt.1: Cannot find the declaration of element 'SCustomerList'.

---------------------

public void validateSchema()
{
String schemaUrl = SCHEMA_URL;
String xmlDocumentUrl = XML_DOCUMENT_URL;

//check for validity with the Scims Xml output dtd
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);

factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
"file://c:/scims-data/sSchema.dtd");
//SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Document doc = null;
try{
DocumentBuilder parser = factory.newDocumentBuilder();
Validator handler=new Validator();
parser.setErrorHandler(handler);

doc = parser.parse("file:///c:/s-data/sxml.xml");
if(handler.validationError==true)
System.out.println("XML Document has Error:"+handler.validationError+" "+handler.saxParseException.getMessage());
else
System.out.println("XML Document is valid");

}
catch (ParserConfigurationException e){
System.out.println("Parser not configured: " + e.getMessage());
}
catch (SAXException e){
System.out.print("Parsing XML failed due to a:\n" + e.getClass().getName()
+ "\n in Line number:" + ((SAXParseException) e).getLineNumber()
+ "\n Coloumn Number:" + ((SAXParseException) e).getColumnNumber()
+ "\n Xml schema file is:" + ((SAXParseException) e).getSystemId());
e.printStackTrace();

System.out.println("\n"+e.getMessage());
}
catch (IOException e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
private class Validator extends DefaultHandler
{
public boolean validationError =false;
public SAXParseException saxParseException=null;
public void error(SAXParseException exception) throws SAXException { validationError = true;
saxParseException=exception;
}
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException=exception;
}
public void warning(SAXParseException exception) throws SAXException
{
}
}


public static void main(String artgs[])
{
DocumentBCValidator docValidator= new DocumentBCValidator();
docValidator.validateSchema();

}



-------------------
Your help would be appreciated.
Thanks

Full Threads Newest First

Showing messages 1 through 2 of 2.

  • Error while validating with a DTD.
    2006-08-03 14:00:43  Salli [View]

    Actually the error is this:

    The markup in the document preceding the root element must be well-formed.

    Earlier I had missed a backslash in specifying the source for the schema document and it was not able to find the DTD file.
    • Error while validating with a DTD.
      2006-08-05 13:00:10  Deepak Vohra | [View]

      The markup in the document preceding the root element must be well-formed.

      Remove any spaces or empty lines before the prolog.