You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Vamsi Atluri <vk...@yahoo.com> on 2003/05/06 16:47:26 UTC

Problem with XML Validation

Hi,

(I posed this on xerces-j-users list, but didn't get any reply yet). I am
using Xerces 2.4.0 for parsing XML files and validating the XML
against the schema file. I created an entity resolver class which loads
the XSD file from the local disk and returns it as an InputStream. I am
setting this entity resolver on the parser in this way:

DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.setEntityResolver(new TestEntityResolver());
parser.setFeature( "http://xml.org/sax/features/validation", true);
parser.setFeature( "http://xml.org/sax/features/namespaces", true);
parser.setFeature( "http://apache.org/xml/features/validation/schema",
true);
parser.setFeature(
"http://apache.org/xml/features/validation/schema-full-checking", 
true);

My entity resolver's resolveEntity method works in this way:

String xsdFile = "TestSchema.xsd";
InputSource is = new InputSource(new FileReader(xsdFile));
return is;

I also created a small ErrorHandler that prints out any 
SAXParseException
that it gets. 

Now, when I try to parse the XML string, I get the following error 
printed
out by my ErrorHandler:

cvc-elt.1: Cannot find the declaration of element 'RootElement'.

The XML String I am trying to parse is of the form:

<RootElement><Elem1>Data1</Elem1><Elem2>Data2</Elem2></RootElement>

My XSD file has the following in the beginning of the XSD file:

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified" attributeFormDefault="unqualified">

And the RootElement is definitely defined in the XSD file. I ran a 
search
for this error through google and some of the discussion boards said 
that
there was a bug with older versions of Xerces, which is supposed to be
fixed. 

Any help would be grately appreciated.

Thank you,
-Vamsi

__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com

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


RE: Problem with XML Validation

Posted by Rahul Srivastava <rs...@firstam.com>.
Hi Vamsi,

 > Vamsi Atluri wrote...
 >
 > Hi,
 >
 > (I posed this on xerces-j-users list, but didn't get any reply yet). I am
 > using Xerces 2.4.0 for parsing XML files and validating the XML
 > against the schema file. I created an entity resolver class which loads
 > the XSD file from the local disk and returns it as an InputStream. I am
 > setting this entity resolver on the parser in this way:
 >
 > DOMParser parser = new org.apache.xerces.parsers.DOMParser();
 > parser.setEntityResolver(new TestEntityResolver());
 > parser.setFeature( "http://xml.org/sax/features/validation", true);
 > parser.setFeature( "http://xml.org/sax/features/namespaces", true);
 > parser.setFeature( "http://apache.org/xml/features/validation/schema",
 > true);
 > parser.setFeature(
 > "http://apache.org/xml/features/validation/schema-full-checking",
 > true);
 >
 > My entity resolver's resolveEntity method works in this way:
 >
 > String xsdFile = "TestSchema.xsd";
 > InputSource is = new InputSource(new FileReader(xsdFile));
 > return is;
 >
 > I also created a small ErrorHandler that prints out any
 > SAXParseException
 > that it gets.
 >
 > Now, when I try to parse the XML string, I get the following error
 > printed
 > out by my ErrorHandler:
 >
 > cvc-elt.1: Cannot find the declaration of element 'RootElement'.

As the message clearly says, the parser is unable to locate a grammar for
the instance.

 >
 > The XML String I am trying to parse is of the form:
 >
 > <RootElement><Elem1>Data1</Elem1><Elem2>Data2</Elem2></RootElement>

I see that, you have not referred to the schemaLocation in the root element.
As a result, your EntityResolver will never be invoked, hence the error. You
can add some println in your EntityResolver to verify, if at all it is being
invoked.

Hope that helps.

Cheers,
Rahul.



 >
 > My XSD file has the following in the beginning of the XSD file:
 >
 > <?xml version="1.0" encoding="UTF-8"?>
 >
 > <xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
 > elementFormDefault="unqualified" attributeFormDefault="unqualified">
 >
 > And the RootElement is definitely defined in the XSD file. I ran a
 > search
 > for this error through google and some of the discussion boards said
 > that
 > there was a bug with older versions of Xerces, which is supposed to be
 > fixed.
 >
 > Any help would be grately appreciated.
 >
 > Thank you,
 > -Vamsi
 >
 > __________________________________
 > Do you Yahoo!?
 > The New Yahoo! Search - Faster. Easier. Bingo.
 > http://search.yahoo.com
 >
 > ---------------------------------------------------------------------
 > To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
 > For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
 >
 >



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


RE: Problem with XML Validation

Posted by Rahul Srivastava <rs...@firstam.com>.
Hi Vamsi,

 > Vamsi Atluri wrote...
 >
 > Hi,
 >
 > (I posed this on xerces-j-users list, but didn't get any reply yet). I am
 > using Xerces 2.4.0 for parsing XML files and validating the XML
 > against the schema file. I created an entity resolver class which loads
 > the XSD file from the local disk and returns it as an InputStream. I am
 > setting this entity resolver on the parser in this way:
 >
 > DOMParser parser = new org.apache.xerces.parsers.DOMParser();
 > parser.setEntityResolver(new TestEntityResolver());
 > parser.setFeature( "http://xml.org/sax/features/validation", true);
 > parser.setFeature( "http://xml.org/sax/features/namespaces", true);
 > parser.setFeature( "http://apache.org/xml/features/validation/schema",
 > true);
 > parser.setFeature(
 > "http://apache.org/xml/features/validation/schema-full-checking",
 > true);
 >
 > My entity resolver's resolveEntity method works in this way:
 >
 > String xsdFile = "TestSchema.xsd";
 > InputSource is = new InputSource(new FileReader(xsdFile));
 > return is;
 >
 > I also created a small ErrorHandler that prints out any
 > SAXParseException
 > that it gets.
 >
 > Now, when I try to parse the XML string, I get the following error
 > printed
 > out by my ErrorHandler:
 >
 > cvc-elt.1: Cannot find the declaration of element 'RootElement'.

As the message clearly says, the parser is unable to locate a grammar for
the instance.

 >
 > The XML String I am trying to parse is of the form:
 >
 > <RootElement><Elem1>Data1</Elem1><Elem2>Data2</Elem2></RootElement>

I see that, you have not referred to the schemaLocation in the root element.
As a result, your EntityResolver will never be invoked, hence the error. You
can add some println in your EntityResolver to verify, if at all it is being
invoked.

Hope that helps.

Cheers,
Rahul.



 >
 > My XSD file has the following in the beginning of the XSD file:
 >
 > <?xml version="1.0" encoding="UTF-8"?>
 >
 > <xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
 > elementFormDefault="unqualified" attributeFormDefault="unqualified">
 >
 > And the RootElement is definitely defined in the XSD file. I ran a
 > search
 > for this error through google and some of the discussion boards said
 > that
 > there was a bug with older versions of Xerces, which is supposed to be
 > fixed.
 >
 > Any help would be grately appreciated.
 >
 > Thank you,
 > -Vamsi
 >
 > __________________________________
 > Do you Yahoo!?
 > The New Yahoo! Search - Faster. Easier. Bingo.
 > http://search.yahoo.com
 >
 > ---------------------------------------------------------------------
 > To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
 > For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
 >
 >



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