Hi Alessandra,
you are already setting up an error handler; you just need to be sure
that the type of the "handler" variable (that we don't see defined) has
overridden the proper virtual methods that are invoked when an error is
reported.
Alberto
Il 13/09/2011 13:29, [email protected] ha scritto:
The following code is the only final version of the algorithm, which returns as
a mandate running errorCount = 6, I have deliberately altered the pattern. Xsd
so that it is different from the xml file. How do I get my printing errors that
occurred during the parsing? Thank you.
int main(int argC, char* argV[])
{
char* filename;
bool dtdValidate = false;
bool xsdValidate = false;
static char* schemaSorgente=0;
char* dtdSorgente;
int i;
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch)
{
XERCES_STD_QUALIFIER cerr<< "Si è verificato un errore in fase di
inizializzazione! :\n"
<< StrX(toCatch.getMessage())<< XERCES_STD_QUALIFIER endl;
return 1;
}
for (i = 1; i< argC; i++) {
//Argomenti in posizione 0 non ci interessa in quanto è relativo
al progetto. I paramentri
//passati in input sono 4 ma è necessario analizzare solo quelli
in posizione 1-2-3
if (!strcmp(argV[i], "-dtd")) {
dtdValidate = true;
dtdSorgente = argV[++i];
} else if (!strcmp(argV[i], "-xsd")) {
xsdValidate = true;
schemaSorgente = argV[++i];
}}
xmlFile = argV[3];
int errorCount = 0;
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgXercesDynamic, false);
parser->setFeature(XMLUni::fgXercesSchema, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
int errorCode = 0;
try
{
parser->setContentHandler(&handler);
parser->setErrorHandler(&handler);
parser->setEntityResolver(&handler);
parser->parse(xmlFile);
errorCount = parser->getErrorCount();
cout<< "Numero errori:"<<errorCount;
}
catch (const OutOfMemoryException&)
{
XERCES_STD_QUALIFIER cerr<< "OutOfMemoryException"<<
XERCES_STD_QUALIFIER endl;
errorCode = 5;
}
catch (const XMLException& toCatch)
{
XERCES_STD_QUALIFIER cerr<< "\nAn error occurred\n Error:"
<< StrX(toCatch.getMessage())
<< "\n"<< XERCES_STD_QUALIFIER endl;
errorCode = 4;
}
if(errorCode) {
XMLPlatformUtils::Terminate();
return errorCode;
}
//
// Delete the parser itself. Must be done prior to calling
Terminate, below.
//
delete parser;
// And call the termination method
XMLPlatformUtils::Terminate();
if (errorCount> 0)
return 4;
else
return 0;
};
----Messaggio originale----
Da: [email protected]
Data: 13-set-2011 8.19
A:<[email protected]>
Ogg: Re: R: RE: Re: Validation documents with SAX in C++
Hi Alessandra,
the location of the file should be a valid URI, so you should replace
spaces with %20
Alberto
Il 12/09/2011 21:52, [email protected] ha scritto:
I replaced
parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,schemaSorgente);
con
parser->setProperty(XMLUni::fgXercesSchemaExternalNoNameSpaceSchemaLocation,schemaSorgente);
I no longer have the error on schemalocator but I get the following error
enable entities to open prumary C: / Documents ..... /?????d???1???. And then I
printed a series of errors such as:
-no declaration found for element personnel.
I think I can not open or find the file. xsd. What could be the problem, run
the code as a console project in VisualStudio 2008.
Any idea on the problem?
----Messaggio originale----
Da: [email protected]
Data: 12-set-2011 14.21
A: "[email protected]"<[email protected]>,
"[email protected]"<[email protected]>
Ogg: RE: Re: Validation documents with SAX in C++
The error is in your XML. If you use the schemaLocation attribute, its value
must consist of namespace and schema location pairs separated by spaces. To
specify just a location use the noNamespaceSchemaLocation attribute instead of
the schemaLocation attribute.
-----Original Message-----
From: [email protected] [mailto:[email protected]]
Sent: Monday, September 12, 2011 8:08 AM
To: [email protected]
Subject: R: Re: Validation documents with SAX in C++
I implemented the following algorithm:
int main(int argC, char* argV[])
{
char* filename;
bool dtdValidate = false;
bool xsdValidate = false;
char* schemaSorgente;
char* dtdSorgente;
int i;
try
{
XMLPlatformUtils::Initialize();
}
catch (const XMLException&amp;amp; toCatch)
{
XERCES_STD_QUALIFIER cerr<< "Si è verificato un errore in fase di
inizializzazione! :\n"
<< StrX(toCatch.getMessage())<< XERCES_STD_QUALIFIER endl;
return 1;
}
for (i = 1; i< argC; i++) {
if (!strcmp(argV[i], "-dtd")) {
dtdValidate = true;
dtdSorgente = argV[++i];
} else if (!strcmp(argV[i], "-xsd")) {
xsdValidate = true;
schemaSorgente = argV[++i];
}}
xmlFile = argV[3];
int errorCount = 0;
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,schemaSorgente);
int errorCode = 0;
try
{
XmlToTestoSAX handler(encodingName, unRepFlags);
// parser->setDocumentHandler(&amp;amp;handler);
parser->setErrorHandler(&amp;amp;handler);
parser->parse(xmlFile);
errorCount = parser->getErrorCount();
}
catch (const OutOfMemoryException&amp;amp;)
{
XERCES_STD_QUALIFIER cerr<< "OutOfMemoryException"<<
XERCES_STD_QUALIFIER endl;
errorCode = 5;
}
catch (const XMLException&amp;amp; toCatch)
{
XERCES_STD_QUALIFIER cerr<< "\nAn error occurred\n Error:"
<< StrX(toCatch.getMessage())
<< "\n"<< XERCES_STD_QUALIFIER endl;
errorCode = 4;
}
if(errorCode) {
XMLPlatformUtils::Terminate();
return errorCode;
}
//
// Delete the parser itself. Must be done prior to calling Terminate,
below.
//
delete parser;
// And call the termination method
XMLPlatformUtils::Terminate();
if (errorCount> 0)
return 4;
else
return 0;
};
but I believe there are errors. Pass the following parameters from the command
line:
-xsd xml\personal.xsd xml\personal-schema.xml
The xml file input is given in the following:
<?xml version="1.0" encoding="UTF-8"?>
<?proc-inst-1 'foo' ?>
<personnel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="C:/Documents and Settings/Alessandra/Documenti/Visual Studio
2008/Projects/XmlToTestoSAXMain/xml/personal.xsd"
>
<person id="Big.Boss">
<name xml:base="foo/bar"><family xml:base="bar/bar">Boss</family> <given
xml:base="car/bar">Big</given><?proc-inst-2 'foobar' ?></name>
<email>[email protected]</email>
<link subordinates="one.worker two.worker three.worker four.worker
five.worker"/>
</person>
<person id="one.worker" xml:base="/auto/bar">
<name xml:base="/car/foo/"><family xml:base="bar/bar">Worker</family>
<given>One</given></name>
<email>[email protected]</email>
<link manager="Big.Boss"/>
</person>
</personnel>
XSD schema is:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:import namespace="http://www.w3.org/XML/1998/namespace">
<xs:annotation>
<xs:documentation>
The schemaLocation of the relevant file is
"http://www.w3.org/2001/xml.xsd"; however,
we don't want to assume people are always
connected to the 'net when playing with this file.
</xs:documentation>
</xs:annotation>
</xs:import>
<xs:element name="personnel">
<xs:complexType>
<xs:sequence>
<xs:element ref="person" minOccurs='1' maxOccurs='unbounded'/>
</xs:sequence>
</xs:complexType>
<xs:unique name="unique1">
<xs:selector xpath="person"/>
<xs:field xpath="name/given"/>
<xs:field xpath="name/family"/>
</xs:unique>
<xs:key name='empid'>
<xs:selector xpath="person"/>
<xs:field xpath="@id"/>
</xs:key>
<xs:keyref name="keyref1" refer='empid'>
<xs:selector xpath="person"/>
<xs:field xpath="link/@manager"/>
</xs:keyref>
</xs:element>
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="email" minOccurs='0' maxOccurs='unbounded'/>
<xs:element ref="url" minOccurs='0' maxOccurs='unbounded'/>
<xs:element ref="link" minOccurs='0' maxOccurs='1'/>
</xs:sequence>
<xs:attribute name="id" type="xs:ID" use='required'/>
<xs:attribute name="note" type="xs:string"/>
<xs:attribute name="contr" default="false">
<xs:simpleType>
<xs:restriction base = "xs:string">
<xs:enumeration value="true"/>
<xs:enumeration value="false"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="salary" type="xs:integer"/>
<xs:anyAttribute namespace="http://www.w3.org/XML/1998/namespace"
processContents="skip"/>
</xs:complexType>
</xs:element>
<xs:element name="name">
<xs:complexType>
<xs:all>
<xs:element ref="family"/>
<xs:element ref="given"/>
</xs:all>
<xs:anyAttribute namespace="http://www.w3.org/XML/1998/namespace"
processContents="skip"/>
</xs:complexType>
</xs:element>
<xs:element name="family">
<xs:complexType>
<xs:simpleContent>
<xs:extension base='xs:string'>
<xs:anyAttribute namespace="http://www.w3.org/XML/1998/namespace"
processContents="skip"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="given">
<xs:complexType>
<xs:simpleContent>
<xs:extension base='xs:string'>
<xs:anyAttribute namespace="http://www.w3.org/XML/1998/namespace"
processContents="skip"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="email" type='xs:string'/>
<xs:element name="url">
<xs:complexType>
<xs:attribute name="href" type="xs:string" default="http://"/>
</xs:complexType>
</xs:element>
<xs:element name="link">
<xs:complexType>
<xs:attribute name="manager" type="xs:IDREF"/>
<xs:attribute name="subordinates" type="xs:IDREFS"/>
</xs:complexType>
</xs:element>
<xs:notation name='gif' public='-//APP/Photoshop/4.0'
system='photoshop.exe'/>
</xs:schema>
When you launch the program running I get the following screen:
Fatal error at file C:\Documents and Settings\Alessandra\Documenti\Visual
Studio 2008\Projects\XmlToTestoSAXMain\xml\personal-schema.xml, line 6, char 6
Message: schemaLocation does not contain name-space location pairs
There are errors in the file. Xml or. Xsd? Furthermore, my algorithm allows to
validate an XML document based on a DTD or XSD supplied from the outside? Thank
you. greetings
----Messaggio originale----
Da: [email protected]
Data: 9-set-2011 8.20
A:<[email protected]>
Ogg: Re: Validation documents with SAX in C++
[email protected] wrote:
Hello, you can have some indication of the methods used to check whether an
XML document conforms to a DTD or XSD with SAX in C++.
Thank you. Alessandra greetings.
Hi,
Please look at the SAX2Count sample source code from the Xerces-C++
source distribution.
Good luck!
Vitaly