XML validation problem
843834Aug 17 2004 — edited Aug 18 2004hi,
i am trying to validate an xml document against a schema document abnd am getting some strange validation errors. i am using xerces to parse. here are my java classes:-
//---------------------------------------- class Validation-------------------------------------------//
import org.apache.xerces.parsers.DOMParser;
public class Validation {
public static void main(String[] args)
{
try
{
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation", true);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation","country.xsd");
ErrorChecker errors = new ErrorChecker();
parser.setErrorHandler(errors);
parser.parse("country.xml");
} // end try
catch (Exception e)
{
System.out.print("Problem parsing the file.");
} // end catch
} // end main
} // end class Validation
//----------------------------------------- class ErrorHandler -----------------------------------------//
import org.xml.sax.*;
public class ErrorChecker implements ErrorHandler
{
public void warning(SAXParseException exception)
{
//Ignore warnings
}
public void error(SAXParseException e)
{
// A validity error.
System.out.println("Vaidation error: " + e.getMessage());
}
public void fatalError(SAXParseException e)
{
// A well-formedness error
System.out.println("Fatal error: " + e.getMessage());
}
} // end class ErrorChecker
and here is my xml document, called "country.xml":-
<?xml version="1.0"?>
<country
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="country.xsd">
<name>France</name>
<pop>59.7</pop>
</country>
and my schema, called "country.xsd":-
<?xml version="1.0"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="country">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="pop" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
and here are the validation errors that are caught when i try to parse the xml document:-
Vaidation error: Document is invalid: no grammar found.
Vaidation error: Document root element "country", must match DOCTYPE root "null".
could anyone please shed any light on these validation errors? any help much appreciated.
thanks,
ramsey