You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by tom john <cy...@yahoo.com> on 2002/04/04 12:36:14 UTC

xml validation with schema ERROR

Hi,
I am new validating xml with schema. I get the
following error message when i try to validate xml:

org.xml.sax.SAXNotRecognizedException:
http://apache.org/xml/feature
s/validation/schema-full-checking
Error:  org.xml.sax.SAXParseException: Document root
element "PERSON
", must match DOCTYPE root "null".
Error:  org.xml.sax.SAXParseException: Document is
invalid: no gramm
ar found.
java.lang.NullPointerException

I know the xml document i have is valid. 
the xml i have is:

<?xml version="1.0" encoding="UTF-8"?>
<PERSON NAME="XXX"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="mySchema.xsd">
	<COUNTRY>countryName</COUNTRY>
	<COUNTRY>countryName</COUNTRY>
</PERSON>

schema for it is:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
	<xs:element name="PERSON">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="COUNTRY" type="xs:string"
maxOccurs="unbounded"/>
			</xs:sequence>
			<xs:attribute name="NAME" type="xs:string"
use="required"/>
		</xs:complexType>
	</xs:element>
</xs:schema>

The code i am using to validate is:

import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import java.io.IOException;

import java.io.File;

//  A Valdating DOM Application
//  with registered Error Handlers
public class SchemaValidate implements ErrorHandler {

    // Constructor
    public SchemaValidate (String xmlFile) {

        //  Create a Xerces DOM Parser
        DOMParser parser = new DOMParser();
        //SAXParser parser = new SAXParser();
        //MyResolver resolver = new MyResolver();

        //  Turn Validation on
        try {
           
parser.setFeature("http://xml.org/sax/features/validation",
true);
           
//parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
"ebxml.xsd");

            //parser.setEntityResolver(resolver);
           
parser.setFeature("http://apache.org/xml/features/validation/schema",false);
           
parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking",false);
           
parser.setFeature("http://apache.org/xml/features/validation/dtd",false);
        }
        catch (SAXNotRecognizedException e) {
            System.err.println (e);
        }
        catch (SAXNotSupportedException e) {
            System.err.println (e);
        }

        //  Register Error Handler
        parser.setErrorHandler (this);

        //  Parse the Document
        try {
            parser.parse(xmlFile);
        }
        catch (SAXException e) {
            System.err.println (e);
        }
        catch (IOException e) {
            System.err.println (e);
        }
        catch (Exception e) {
            System.err.println (e);
        }

    }

    //  Warning Event Handler
    public void warning (SAXParseException e)
        throws SAXException {
        System.err.println ("Warning:  "+e);
    }

    //  Error Event Handler
    public void error (SAXParseException e)
        throws SAXException {
        System.err.println ("Error:  "+e);
    }

    //  Fatal Error Event Handler
    public void fatalError (SAXParseException e)
        throws SAXException {
        System.err.println ("Fatal Error:  "+e);
    }

    // Main Method
    public static void main (String[] args) {
        SchemaValidate validatingDOM = new
SchemaValidate("D:\\tmp\\myxml.xml");
    }
}

thankx

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org