Thankyou both for your quick responses.
I have altered the code appropriately and now my custom error handler
(plagiarised from samples/DOMPrint/DOMTreeErrorReporter) correctly
identifies the invalid xml files. Altered code below so anyone searching
the archives for a quick simple solution can find it:
//initialisation stuff
XercesDOMParser parser;
parser.setValidationScheme(XercesDOMParser::Val_Always);
parser.setDoNamespaces(true);
parser.setDoSchema(true);
ErrorReporter *errReporter = new ErrorReporter();//see
/samples/DOMPrint/DOMTreeErrorReporter - just used functionality
provided in that
// The object parser calls when it detects violations of the schema.
parser.setErrorHandler(errReporter);
//schema file - location is in testschema string
parser.loadGrammar(testschema.c_str(),
Grammar::SchemaGrammarType, true);
try//load file with surplus info - should load OK
{
parser.parse(testfile.c_str());
}
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 (...) {
cout << "Unexpected Exception \n" ;
}
//other tests for invalid files - errorhandler prints errors to
stdout, no exceptions caught in try/catch block as above
Alberto Massari wrote:
Hi Martin,
in order to get the validation errors you need to set the error
handler; otherwise you can only ask parser.getErrorCount() to detect
whether non-fatal errors occurred.
Alberto
At 12.35 18/04/2007 +0100, martin waller wrote:
Hi,
I'm trying to work out how to validate xml files with a simple schema
I've written and I'm clearly missing something.
Here's the code that I'm using to do the validation:
//initialisation stuff...
XercesDOMParser parser;
parser.setValidationScheme(XercesDOMParser::Val_Always);
parser.setDoNamespaces(true);
parser.setDoSchema(true);
parser.setExternalNoNamespaceSchemaLocation(testschema.c_str());//the
schema..
try
{
parser.parse(testfile.c_str());
}
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 (...) {
cout << "Unexpected Exception \n" ;
}
try
{
parser.parse(testfile2.c_str());
}
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 (...) {
cout << "Unexpected Exception \n" ;
}
'testfile' should be valid, and testfile2 isn't, but when I run the
program 'nothing happens' (it runs, no exceptions, no complaints).
Here's the xsd file:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="componentDefinition">
<xsd:complexType>
<xsd:all>
<xsd:element name="refDes" type="xsd:string" minOccurs="0"
maxOccurs="1"/>
<xsd:element name="packageName" type="xsd:string"
maxOccurs="1"/>
<xsd:element name="partNumber" type="xsd:string"
minOccurs="0" maxOccurs="1"/>
<xsd:element name="power" type="xsd:float" maxOccurs="1"/>
<xsd:element name="powerUnits" type="xsd:string"
minOccurs="0" maxOccurs="1"/>
<xsd:element name="zRot">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="0"/>
<xsd:enumeration value="90"/>
<xsd:enumeration value="180"/>
<xsd:enumeration value="270"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="length" type="xsd:float" maxOccurs="1"/>
<xsd:element name="width" type="xsd:float" maxOccurs="1"/>
<xsd:element name="lengthUnits" type="xsd:string"
minOccurs="0" maxOccurs="1"/>
<xsd:element name="height" type="xsd:float" maxOccurs="1"/>
<xsd:element name="heightUnits" type="xsd:string"
minOccurs="0" maxOccurs="1"/>
</xsd:all>
<xsd:element name="property" minOccurs="0" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="propName" type="xsd:string"/>
<xsd:element name="propValue" type="xsd:string"/>
<xsd:element name="propUnits" type="xsd:string"
minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Here's the invalid xml file testfile2):
<?xml version="1.0" encoding="utf-8" ?>
<componentDefinition xmlns:xsi =
"http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="CompDef.xsd">
<refDes>U1</refDes>
<rubbish>Test01-02-03</rubbish>
<power>3</power>
<powerUnits>W</powerUnits>
<zRot>0</zRot>
<length>100</length>
<width>50</width>
<lengthUnits>mm</lengthUnits>
<height>4</height>
<heightUnits>mm</heightUnits>
</componentDefinition>
I'm not quite sure what to expect when it parses the invalid file? It
seems to do it OK then just returns normally. Shuld it throw an
exception or what? I'm a bit overwhelmed by the docs...like I say,
I'm new to this so please treat me as a beginner...
Martin
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________