An example showing XML serialization that is performed using SAX2.
#include <iostream>
using namespace zorba;
{
protected:
std::ostream& theOStream;
bool preserveWhiteSpaces;
public:
XMLSerializer( std::ostream & aOStream, bool useWS = true )
: theOStream( aOStream ),
preserveWhiteSpaces( useWS ){}
virtual ~XMLSerializer(){}
void endDocument()
{
theOStream << std::endl;
}
void startElement(
const String &uri,
const String &localname,
{
theOStream << "<" << qname;
for (
unsigned int i = 0; i < attrs.
getLength(); i++ ) {
theOStream <<
" " << attrs.
getQName( i ) <<
"=\"" << attrs.
getValue( i ) <<
"\"";
}
theOStream << ">";
}
{
theOStream << "</" << qname << ">";
}
void characters(
const String & text )
{
theOStream << text;
}
void processingInstruction(
const String &target,
const String &data ){}
void ignorableWhitespace(
const String & whitespace )
{
if ( preserveWhiteSpaces ) {
theOStream << whitespace;
}
}
};
int sax2( int argc, char * argv[] )
{
XMLSerializer lContentHandler( std::cout );
try
{
XQuery_t lQuery = lZorba->
compileQuery(
"<a xmlns:f=\"foo\" xmlns=\"http://zorba-xquery.com/defaultns\"> text a text a <b xmlns:ns1=\"http://zorba-xquery.com/usecase1\" attr1=\"value1\" attr2=\"value2\"> text b </b><f:bar>foo</f:bar><foo /><bar /><b><![CDATA[ foo ]]></b></a>");
lQuery->registerSAXHandler( &lContentHandler );
lQuery->executeSAX();
}
{
std::cerr << e << std::endl;
}
return 0;
}