You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Shalmi <sh...@gmail.com> on 2007/08/24 08:20:46 UTC
XML validations using XSD
Hi,
I'm new to XML and xerces field. I want to write a code that will validate
"Books.xml" against "Books.xsd".
I have tried doing the following but I always get SAXException. Please guide
me whether I'm missing some steps or doing some thing wrong( or just in case
if the i/p XML and XSD file are wrong)
Code :-
Try 1 -
try{
InputSource * is = new LocalFileInputSource((XalanDOMChar
*)"http://www.w3.org/2001/XMLSchema");
parser->setValidationScheme(XercesDOMParser::Val_Always);
parser->setDoNamespaces(true);
parser->setDoValidation(true);
parser->setDoSchema(true);
ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
parser->setErrorHandler(errHandler);
parser->setExternalNoNamespaceSchemaLocation("Books.xsd");
parser->loadGrammar(*is,Grammar::SchemaGrammarType, true);
parser->parse("Books.xml");
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
}
catch (const DOMException& toCatch) {
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
}
catch(const SAXParseExceptionType &theException)
{
cerr << "SAXParseExceptionType exception: ";
cerr<<theException.getLineNumber()<<endl;
cerr<<theException.getSystemId ()<<endl;
}
catch(...)
{
cerr << "Generic exception caught!" << endl;
return -1;
}
------------------------------------------------------------------------------------------------------
Try 2 -
ErrorHandler * handler = new HandlerBase ();
// XalanSourceTreeParserLiaison TheParser;
XalanSourceTreeDOMSupport theDOMSupport(TheParser);
try{
XMLPlatformUtils::Initialize();
//XercesParserLiaison TheParser;// One more doubt if i use this instead of
XalanSourceTreeParserLiaison i get runtime error of calling pure virtual
function.Why??
XalanSourceTreeParserLiaison TheParser;
const XalanDOMString theXSDName("Books.xsd");
TheParser.setExternalSchemaLocation(theXSDName.c_str());
const XalanDOMString theXSDURIName("http://www.w3.org/2001/XMLSchema");
TheParser.setUseValidation(true);
TheParser.setDoNamespaces(true);
TheParser.setErrorHandler (handler);
TheParser.setExternalNoNamespaceSchemaLocation(theXSDURIName.c_str());
const XalanDOMChar * temp1 =
TheParser.getExternalNoNamespaceSchemaLocation();
XalanDOMString temp (temp1);
cout<<temp;
const XalanDOMString theFileName("Books.xml");
// Create an input source that represents a local file...
const LocalFileInputSource theInputSource(theFileName.c_str());
XalanDocument * ParsedDocument =
TheParser.parseXMLStream(theInputSource,theXSDName);
if(ParsedDocument != NULL)
cout<<"Valid XML"<<endl;
else
cout<<"Invalid XML"<<endl;
TheParser.destroyDocument(ParsedDocument);
XMLPlatformUtils::Terminate();
}
catch(const SAXParseExceptionType &theException)
{
cerr << "SAXParseExceptionType exception: ";
cerr<<theException.getLineNumber()<<endl;
cerr<<theException.getSystemId ()<<endl;
return -1;
}
catch(...)
{
cerr << "Generic exception caught!" << endl;
return -1;
}
http://www.nabble.com/file/p12307218/Books.xml Books.xml
http://www.nabble.com/file/p12307218/Books.xsd Books.xsd
--
View this message in context: http://www.nabble.com/XML-validations-using-XSD-tf4321874.html#a12307218
Sent from the Xerces - C - Users mailing list archive at Nabble.com.