Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Xerces Validation using SAXParser & Schema

Status
Not open for further replies.

michaelkrauklis

Programmer
Dec 5, 2001
226
0
0
US
I'm having a problem validating an xml file using a Schema and the Xerces SAXParser. The code is as follows:
Code:
	parser = new SAXParser();
	parser->setDoNamespaces(true);
	parser->setValidationScheme(SAXParser::Val_Always);
	parser->setValidationSchemaFullChecking(true);
	parser->setExternalSchemaLocation("world.xsd");


	parser->setDocumentHandler(this);
	parser->setErrorHandler(this);


	try{
		cout<<&quot;--Parsing--&quot;<<endl;
		const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
		parser->parse(systemid);
		const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
		cout<<endl<<endl<<&quot;Parse duration:  &quot;<<(endMillis - startMillis)<<&quot;ms.&quot;<<endl;
//*************************************************************************************************

I believe the problem is it's not loading the Schema correctly, so when I parse and it tries to validate it sends an error each time it reads an element or attribute because it's not expecting anything. world.xsd and the systemid(world.xml) are both in the directory in which the application is running. I'm using VC++6.0 Any help will be greatly appreciated. Thanks.

p.s. This is obviously in C++, but the function in which I'm running this is a member function of a class that implements the DocumentHandler and ErrorHandler interfaces, hence the this reference when setting the DocumentHandler and ErrorHandler for the parser. MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 

Enigma: I am using the Xercess' DOMParser, which has problems on its turn. Actually, I've never succeeded in validating an instance with their parser, instance that gets validated with XMLSpy - it's true, against a large and complicated set of schemas but... they say it should work.

However, make sure that the schema path is correct (aka. full path) or else, the program runs in the directory where the schema resides. I had these problems with the DOMParser, I wonder if the SAX parser have them too. [red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
The schema path is correct(both the schema file and the xml file reside in the same directory as the application), and the xml file is a generated sample file created automaticaly using XMLSpy, so I know the xml file should be valid. Has anyone had any luck passing a SchemeValidator object to the DOMParser or SAXParser constructor? When I do that I get an exception of unknown type. And aren't there applications out there using xerces that validate using schema? It has to be possible, and someone out there has to have done it. Thanks for the advice. MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
Be careful with schema generated from XMLSpy. Their schema generator is good compared to others, but still often makes bad assumptions about your data model. Look especially for attributes and values that are handled by enumerated lists where you really just wanted any xs:string or xs:NMTOKEN, etc. Also, if you are using your own namespace, look for a xmlns:stuff attribute (where 'stuff' is your namespace prefix) on your document node. If you are using your own namespace there are a few things you have to do before XMLSpy will even recognize it's own generated schema as valid... ^_^

Hope this helps,
Uura
~~~~
&quot;Common sense tells you that the world is flat.&quot;
 
I don't think I have any weird namespace things going on. They're pretty standard files, both the schema and the xml document. I'm trying to get some basic validation working before I move on to anything harder. Both the xml document and the schema I'm using can be found in . As you can see, nothing spectacular. And I don't think it's a problem with the schema, because it's not even finding the root element properly. grrr, frusturations... MYenigmaSELF:-9
myenigmaself@myenigmaself.gaiden.com
&quot;If debugging is the process of removing bugs, then programming must be the process of putting them in.&quot; --Dykstra
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top