473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Validation of XML file against external XSD Schema using Xerces CDT

Hi Everyone,

I am currently implementing an XercesDOMParser to parse an XML file and
to validate this file against its XSD Schema file which are both
located on my local HD drive. For this purpose I set the corresponding
XercesDOMParser feature as shown in the upcoming subsection of my code.

As far as I understand, the parsing process should throw an
DOMException in case the XML file doesn't match the Schema file (e.g.
Element declarations mismatch..) but in my case nothing happens at all.
I even set parser->setValidationConstraintFatal(true) but this also
doesnt help. Have i missed to implement some certain Exception Handler?
Does anybody has an idea what I am doing wrong?

XercesDOMParser* XMLparser = new XercesDOMParser();
// optionally set some features
XMLparser->setDoNamespaces(true);
XMLparser->setDoSchema(true);
XMLparser->setDoValidation(true);
XMLparser->setValidationScheme(XercesDOMParser::Val_Always );
XMLparser->setExternalNoNamespaceSchemaLocation(myXSDfilepat h);
XMLparser->setValidationSchemaFullChecking(true);
XMLparser->setValidationConstraintFatal(true);

//Define Error Handler
ErrorHandler *errHandler = (ErrorHandler*) new HandlerBase();
XMLparser->setErrorHandler(errHandler);

//parsing the according XML file
try
{
XMLparser->parse(myXMLfilepath);
}
//Exception Handling if parsing fails
catch (const XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}

PLEASE! Its urgent.. Can anyone give me at least a hint?
Thanks in advance for response..

Oct 11 '06 #1
7 10232
Hi Christian!

What do you mean with nothing happens? Is ErrorHandler of type
DOMErrorHandler?

Usually you get called in the error handler you set with
setErrorHandler() for each error in the XML instance. Is the schema
referrenced in the XML file? What happens if you remove the
setExternalNoNamespaceSchemaLocation() statement? Unfortunately I don't
have the code available now but I can take a look in the evening for
the details.

Regards

spiff

http://www.spycomponents.com
ValidatorBuddy - A tool for using different XML validators
ch****************@nt.uni-saarland.de schrieb:
Hi Everyone,

I am currently implementing an XercesDOMParser to parse an XML file and
to validate this file against its XSD Schema file which are both
located on my local HD drive. For this purpose I set the corresponding
XercesDOMParser feature as shown in the upcoming subsection of my code.

As far as I understand, the parsing process should throw an
DOMException in case the XML file doesn't match the Schema file (e.g.
Element declarations mismatch..) but in my case nothing happens at all.
I even set parser->setValidationConstraintFatal(true) but this also
doesnt help. Have i missed to implement some certain Exception Handler?
Does anybody has an idea what I am doing wrong?

XercesDOMParser* XMLparser = new XercesDOMParser();
// optionally set some features
XMLparser->setDoNamespaces(true);
XMLparser->setDoSchema(true);
XMLparser->setDoValidation(true);
XMLparser->setValidationScheme(XercesDOMParser::Val_Always );
XMLparser->setExternalNoNamespaceSchemaLocation(myXSDfilepat h);
XMLparser->setValidationSchemaFullChecking(true);
XMLparser->setValidationConstraintFatal(true);

//Define Error Handler
ErrorHandler *errHandler = (ErrorHandler*) new HandlerBase();
XMLparser->setErrorHandler(errHandler);

//parsing the according XML file
try
{
XMLparser->parse(myXMLfilepath);
}
//Exception Handling if parsing fails
catch (const XMLException& toCatch)
{
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}

PLEASE! Its urgent.. Can anyone give me at least a hint?
Thanks in advance for response..
Oct 11 '06 #2
Hello spiff,

thanks A LOT for your fast response. First of all, I should mention
that I am a totally newbie in Xerces so that i most probably did a
quite elementary mistake.
What do you mean with nothing happens?
In terms of nothing happens means, when i manipulate either the schema
file or the xml file in a way that they become inconsistently, I still
dont get any Exception call. I thought that if the validation process
fails, the parsing method throws an DOMException which I can catch with
the follwing section... ?

catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}
Is ErrorHandler of type DOMErrorHandler?
The Error Handler itself is not of type DOMErrorHandler cause when I
want to parse an DOMErrorHandler to the setErrorHandler() function, I
get an error message roughly saying:

(translated..) Error: no suitable function call for
xercesc_2_7::XercesDOMParser::setErrorHandler(xerc esc_2_7::DOMErrorHandler*&)«
... even though I have included <xercesc/dom/DOMErrorHandler.hpp>... Is
there another way of assigning this ErrorHandler to the parser?? By the
way, is it mandatory to instantiate my own error handler methods to
just indicate a validation problem? I dont necessarily need to know
where the error as happened nor what error. I just want to know VALID
or NOT VALID..
>What happens if you remove the setExternalNoNamespaceSchemaLocation() statement?
In terms of the setExternalNoNamespaceSchemaLocation() statement I just
wanted to assign the pathname of my external .xsd file
("/home/eickhoff/myfile.xsd"). What other way could I make use of to
tell the parser where to find my external schema? As I finally seem to
have made a more trivial mistake, this function has no influence on the
performance at all -means if I erase it, the behaviour of the code is
still the same.

Other solutions than mine to easily validate an XML file against its
external Schema are highly appreciated as well!!

Best regards,
Christian A. Eickhoff

Oct 11 '06 #3
Christian,

I'm sorry but I don't have the code available now but I will post it
this (late) evening (european time).

I'm not aware of any other method to know if the XML is valid then
taking a look at the DOMErrorHandler exceptions. If you didn't get one
the XML is valid. And I also don't know any other method to set the
error handler.

The schema could also assigned in the XML instance itself. In fact I
believe this is the more common case.

Maybe try to get it running with a real DOMErrorHandler in the
meantime.

spiff

http://www.spycomponents.com
ValidatorBuddy - A tool for using different XML validators
ch****************@nt.uni-saarland.de schrieb:
Hello spiff,

thanks A LOT for your fast response. First of all, I should mention
that I am a totally newbie in Xerces so that i most probably did a
quite elementary mistake.
What do you mean with nothing happens?

In terms of nothing happens means, when i manipulate either the schema
file or the xml file in a way that they become inconsistently, I still
dont get any Exception call. I thought that if the validation process
fails, the parsing method throws an DOMException which I can catch with
the follwing section... ?

catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}
Is ErrorHandler of type DOMErrorHandler?

The Error Handler itself is not of type DOMErrorHandler cause when I
want to parse an DOMErrorHandler to the setErrorHandler() function, I
get an error message roughly saying:

(translated..) Error: no suitable function call for
xercesc_2_7::XercesDOMParser::setErrorHandler(xerc esc_2_7::DOMErrorHandler*&)«
.. even though I have included <xercesc/dom/DOMErrorHandler.hpp>... Is
there another way of assigning this ErrorHandler to the parser?? By the
way, is it mandatory to instantiate my own error handler methods to
just indicate a validation problem? I dont necessarily need to know
where the error as happened nor what error. I just want to know VALID
or NOT VALID..
What happens if you remove the setExternalNoNamespaceSchemaLocation() statement?

In terms of the setExternalNoNamespaceSchemaLocation() statement I just
wanted to assign the pathname of my external .xsd file
("/home/eickhoff/myfile.xsd"). What other way could I make use of to
tell the parser where to find my external schema? As I finally seem to
have made a more trivial mistake, this function has no influence on the
performance at all -means if I erase it, the behaviour of the code is
still the same.

Other solutions than mine to easily validate an XML file against its
external Schema are highly appreciated as well!!

Best regards,
Christian A. Eickhoff
Oct 11 '06 #4
Christian,

to define my own DOMErrorHandler class I use the following #includes in
the header file:

#include <xercesc/dom/DOMErrorHandler.hpp>
#include <xercesc/dom/DOMLocator.hpp>

XERCES_CPP_NAMESPACE_USE
This code snippet should do the validation for you. Please note that I
don't set the schema instance here. So you might want to add this on
your own:

// Initialize the XML4C system
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
strGeneralResult.Format(_T("Error during Xerces
initialization! :\n%s\n"), toCatch.getMessage());
return;
}

// Instantiate the DOM parser.
static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };

DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(gL S);
DOMBuilder* parser = ((DOMImplementationLS
*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHR ONOUS, 0);

parser->setFeature(XMLUni::fgDOMNamespaces, true);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);

parser->setFeature(XMLUni::fgDOMValidation, true);

// enable datatype normalization - default is off
parser->setFeature(XMLUni::fgDOMDatatypeNormalization, true);

// And create our error handler and install it
// CMyDOMErrorHandler must be derived from DOMErrorHandler
parser->setErrorHandler(new CMyDOMErrorHandler);

//
// Get the starting time and kick off the parse of the indicated
// file. Catch any exceptions that might propogate out of it.
//
unsigned long duration;

XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* doc = 0;

try
{
// reset document pool
parser->resetDocumentPool();

doc = parser->parseURI(_T("C:\your_file.xml"));
}
catch (const XMLException& toCatch)
{
strGeneralResult.Format(_T("Error during parsing: %s\nXML
Exception message is:\n %s\n"), strXMLFile, toCatch.getMessage());
}
catch (const DOMException& toCatch)
{
const unsigned int maxChars = 2047;
XMLCh errText[maxChars + 1];

strGeneralResult.Format(_T("DOM Error during parsing:
%s\nDOMException code is: %d\n"), strXMLFile, toCatch.code);

if (DOMImplementation::loadDOMExceptionMsg(toCatch.co de,
errText, maxChars))
{
CString strDOMExc;
strDOMExc.Format(_T("\nMessage is: %s"), errText);

strGeneralResult +=strDOMExc;
}
}
catch (...)
{
strGeneralResult.Format(_T("Unexpected exception during
parsing: %s\n"), strXMLFile);
}
Regards
spiff wrote:
Christian,

I'm sorry but I don't have the code available now but I will post it
this (late) evening (european time).

I'm not aware of any other method to know if the XML is valid then
taking a look at the DOMErrorHandler exceptions. If you didn't get one
the XML is valid. And I also don't know any other method to set the
error handler.

The schema could also assigned in the XML instance itself. In fact I
believe this is the more common case.

Maybe try to get it running with a real DOMErrorHandler in the
meantime.

spiff

http://www.spycomponents.com
ValidatorBuddy - A tool for using different XML validators
ch****************@nt.uni-saarland.de schrieb:
Hello spiff,

thanks A LOT for your fast response. First of all, I should mention
that I am a totally newbie in Xerces so that i most probably did a
quite elementary mistake.
What do you mean with nothing happens?
In terms of nothing happens means, when i manipulate either the schema
file or the xml file in a way that they become inconsistently, I still
dont get any Exception call. I thought that if the validation process
fails, the parsing method throws an DOMException which I can catch with
the follwing section... ?

catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}
Is ErrorHandler of type DOMErrorHandler?
The Error Handler itself is not of type DOMErrorHandler cause when I
want to parse an DOMErrorHandler to the setErrorHandler() function, I
get an error message roughly saying:

(translated..) Error: no suitable function call for
xercesc_2_7::XercesDOMParser::setErrorHandler(xerc esc_2_7::DOMErrorHandler*&)«
.. even though I have included <xercesc/dom/DOMErrorHandler.hpp>... Is
there another way of assigning this ErrorHandler to the parser?? By the
way, is it mandatory to instantiate my own error handler methods to
just indicate a validation problem? I dont necessarily need to know
where the error as happened nor what error. I just want to know VALID
or NOT VALID..
>What happens if you remove the setExternalNoNamespaceSchemaLocation() statement?
In terms of the setExternalNoNamespaceSchemaLocation() statement I just
wanted to assign the pathname of my external .xsd file
("/home/eickhoff/myfile.xsd"). What other way could I make use of to
tell the parser where to find my external schema? As I finally seem to
have made a more trivial mistake, this function has no influence on the
performance at all -means if I erase it, the behaviour of the code is
still the same.

Other solutions than mine to easily validate an XML file against its
external Schema are highly appreciated as well!!

Best regards,
Christian A. Eickhoff
Oct 11 '06 #5
Hey spiff,

thanks a lot for your source code subsection. Now that I use the
DOMBuilder instead of XercesDOMParser, i can set my own ErrorHandler
smoothly and the validation process works fine :-).. The only problem I
still had was the external Schema location but as I inserted the
xsi:noNamespaceSchemaLocation within the root tag of my XML file,
everything works fine!!

Thanks again!! You really helped me out a lot :-)))

Regards,
Christian

Oct 12 '06 #6
It was a pleasure :-)

spiff
ch****************@nt.uni-saarland.de schrieb:
Hey spiff,

thanks a lot for your source code subsection. Now that I use the
DOMBuilder instead of XercesDOMParser, i can set my own ErrorHandler
smoothly and the validation process works fine :-).. The only problem I
still had was the external Schema location but as I inserted the
xsi:noNamespaceSchemaLocation within the root tag of my XML file,
everything works fine!!

Thanks again!! You really helped me out a lot :-)))

Regards,
Christian
Oct 12 '06 #7
Hello Christian!

Some time ago I developed a little tool that does what you want.
I compiled it on a linux system using xerces-c2.6.0.
Usage:
xsdval XSDSchema XML_file

I hadn't time to examine the code in your posting but I think
you may easily figure out what's wrong looking at the attached sources of
xsdval.
regards,
Oskar Stuffer

ch****************@nt.uni-saarland.de wrote:
Hello spiff,

thanks A LOT for your fast response. First of all, I should mention
that I am a totally newbie in Xerces so that i most probably did a
quite elementary mistake.
>What do you mean with nothing happens?

In terms of nothing happens means, when i manipulate either the schema
file or the xml file in a way that they become inconsistently, I still
dont get any Exception call. I thought that if the validation process
fails, the parsing method throws an DOMException which I can catch with
the follwing section... ?

catch (const DOMException& toCatch)
{
char* message = XMLString::transcode(toCatch.msg);
cout << "Exception during parsing. " << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...)
{
cout << "Unexpected Exception \n";
return -1;
}
>Is ErrorHandler of type DOMErrorHandler?

The Error Handler itself is not of type DOMErrorHandler cause when I
want to parse an DOMErrorHandler to the setErrorHandler() function, I
get an error message roughly saying:

(translated..) Error: no suitable function call for
xercesc_2_7::XercesDOMParser::setErrorHandler(xerc esc_2_7::DOMErrorHandler*&)«
.. even though I have included <xercesc/dom/DOMErrorHandler.hpp>... Is
there another way of assigning this ErrorHandler to the parser?? By the
way, is it mandatory to instantiate my own error handler methods to
just indicate a validation problem? I dont necessarily need to know
where the error as happened nor what error. I just want to know VALID
or NOT VALID..
>What happens if you remove the setExternalNoNamespaceSchemaLocation() statement?

In terms of the setExternalNoNamespaceSchemaLocation() statement I just
wanted to assign the pathname of my external .xsd file
("/home/eickhoff/myfile.xsd"). What other way could I make use of to
tell the parser where to find my external schema? As I finally seem to
have made a more trivial mistake, this function has no influence on the
performance at all -means if I erase it, the behaviour of the code is
still the same.

Other solutions than mine to easily validate an XML file against its
external Schema are highly appreciated as well!!

Best regards,
Christian A. Eickhoff

FILES = xsdval.cpp ErrorReporter.cpp
HEADERS = ErrorReporter.hh
PROG = xsdval

OBJECTS = $(FILES:.cpp=.o)

# path of xerces library
XERCESROOT = /home/oskar/software/languages/C++/libraries/xerces-c-src_2_6_0

INCLUDEDIRS = -I $(XERCESROOT)/include
LIBDIRS = -L $(XERCESROOT)/lib
LIBS = -lxerces-c
CC = g++
CFLAGS = -g $(INCLUDEDIRS)
INSTALLDIR = /opt/paghe6

..PHONY: clean cleantags install all

all: $(PROG)

$(PROG): $(OBJECTS) Makefile
$(CC) $(CFLAGS) $(LIBDIRS) -o $(PROG) $(OBJECTS) $(LIBS)

$(OBJECTS): %.o: %.cpp $(HEADERS)
$(CC) -c $(CFLAGS) $< -o $@

clean:
rm -f *.o $(PROG)

TAGS: $(FILES)
etags $(FILES) $(HEADERS)

cleantags:
@rm -f TAGS

install: all
strip $(PROG)
cp $(PROG) $(INSTALLDIR)/bin

Oct 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Zandy Marantal | last post by:
Hello everyone, I'm having trouble using Xerces2(2.4, 2.5) when validating against an XML schema if a general entity reference is defined within the XML file. The error I'm getting is this:...
1
by: Ole Hedegaard | last post by:
Hi, I've been looking into the Xerces documentation, and to my surprise it seems that it is impossible to validate an XML document againts an XML Schema without having a schemaLocation (or...
2
by: Olaf Meyer | last post by:
Apprentently xerces 2.6.0 (Java) does not validate against contraints specified in the schema (e.g. constraints specified via unique element). The validation works with the XML editor I'm using...
0
by: Brian | last post by:
I am having alot of trouble getting a XML document validated with a schema. I got a sample document and schema off of w3schools.com, which passed an online xml validator:...
0
by: Aaron P Frenger | last post by:
Hello All, I have a very large XML file that I would like to split up into a few smaller files, but still use only one schema. I am using Xerces C++ libraries. My idea is to have one schema...
4
by: Oskar Stuffer | last post by:
Hi! I'm using the msv tool which uses xerces to validate a XML document against a XML Schema definition. This is my XML Schema definition: <?xml version="1.0" encoding="ISO-8859-1"?>...
0
by: Aaron P Frenger | last post by:
Hello All, I have a very large XML file that I would like to split up into a few smaller files, but still use only one schema. I am using Xerces C++ libraries. My idea is to have one schema...
1
by: Nuno | last post by:
Hello, I'm looking for a way of validating/parsing the xsd file (schema), i only been able of validating the xml file with the corresponding schema, but what i want is only validate the xsd...
9
by: mstilli | last post by:
Hi, I am trying to use schema for server side validation using xerces to catch the validation errors. validating this XML: <Content4> <textarea13></textarea13>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.