Doesn't anyone here do schema validation/parsing with DOM?  I am
really trying to find *something* helpful on the net but am shooting
blanks here.
If I modify my authNotify.xml document in anyway that goes against the
XSD, the parsing seems to be successful no matter what.

********************************************************************************
authNotify.xsd
********************************************************************************

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";>
    <xs:element name="authNotify">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="transaction" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="TrimmedStringType">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:whiteSpace value="collapse"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>

    <xs:simpleType name="StationCodeType">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A-Z][A-Z][0-9]{4}"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:simpleType name="StartCodeType">
        <xs:restriction base="xs:integer">
            <xs:pattern value="99[0-9]{4}"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:element name="poNum" type="xs:integer" />
    </xs:element>

    <xs:element name="startCode" type="StartCodeType" />

    <xs:element name="stationCode" type="StationCodeType" />

    <xs:element name="stationDate" type="xs:date" />

    <xs:element name="stationTime" type="xs:time" />

    <xs:element name="transaction">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="stationCode" />
                <xs:element ref="stationDate" />
                <xs:element ref="stationTime" />
                <xs:element ref="poNum" />
                <xs:element ref="startCode" />
            </xs:sequence>
            <xs:attribute name="msgId" type="xs:NMTOKEN" use="required" />
        </xs:complexType>
    </xs:element>

</xs:schema>

********************************************************************************
authNotify.xml
********************************************************************************
<?xml version="1.0" encoding="UTF-8" standalone="no"
?><authNotify><transaction
msgId="MESSAGE-ID"><stationCode>TN0001</stationCode><stationDate>2009-06-08</stationDate><stationTime>15.45.49</stationTime><poNum>13462</poNum><startCode>997232</startCode></transaction></authNotify>


********************************************************************************
parse.cpp
********************************************************************************
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLUni.hpp>

#if defined(XERCES_NEW_IOSTREAMS)
#include <iostream>
#else
#include <iostream.h>
#endif

XERCES_CPP_NAMESPACE_USE

int main (int argc, char* args[])
{
    try {
        XMLPlatformUtils::Initialize();
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Error during initialization! :\n" << message << "\n";
        XMLString::release(&message);
        return 1;
    }

    XercesDOMParser* parser = new XercesDOMParser();
    try {
        // My compiler complains about using
Grammar::SchemaGrammarType in the second parameter to loadGrammar()
        // because Grammar is an incomplete class.
        // Judging by the enum SchemaGrammarType should be 1.

        parser->loadGrammar("/home/kbeard/authNotify.xsd", 1);
        parser->setValidationScheme(XercesDOMParser::Val_Always);
        parser->setDoNamespaces(true);    // optional
        parser->setDoSchema(true);    // optional
        parser->setValidationSchemaFullChecking(true);
        parser->useScanner(XMLUni::fgSGXMLScanner);
    }
    catch (...) {
        cout << "shit happened" << endl;
    }

    ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
    parser->setErrorHandler(errHandler);

    char* xmlFile = "/home/kbeard/authNotify.xml";

    try {
        parser->parse(xmlFile);
    }
    catch (const XMLException& toCatch) {
        char* message = XMLString::transcode(toCatch.getMessage());
        cout << "Exception message is: \n" << message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (const DOMException& toCatch) {
        char* message = XMLString::transcode(toCatch.msg);
        cout << "Exception message is: \n" << message << "\n";
        XMLString::release(&message);
        return -1;
    }
    catch (...) {
        cout << "Unexpected Exception \n" ;
        return -1;
    }

    cout << "shit worked?" << endl;

    delete parser;
    delete errHandler;
    return 0;
}


-- 
Kelly Beard

Reply via email to