📄 qxml.cpp
字号:
*//*! \fn bool QXmlReader::hasProperty( const QString& name ) const Returns TRUE if the reader has the property \a name, otherwise FALSE. \sa property() setProperty()*//*! \fn void QXmlReader::setEntityResolver( QXmlEntityResolver* handler ) Sets the entity resolver to \a handler. \sa entityResolver()*//*! \fn QXmlEntityResolver* QXmlReader::entityResolver() const Returns the entity resolver or 0 if none was set. \sa setEntityResolver()*//*! \fn void QXmlReader::setDTDHandler( QXmlDTDHandler* handler ) Sets the DTD handler to \a handler. \sa DTDHandler()*//*! \fn QXmlDTDHandler* QXmlReader::DTDHandler() const Returns the DTD handler or 0 if none was set. \sa setDTDHandler()*//*! \fn void QXmlReader::setContentHandler( QXmlContentHandler* handler ) Sets the content handler to \a handler. \sa contentHandler()*//*! \fn QXmlContentHandler* QXmlReader::contentHandler() const Returns the content handler or 0 if none was set. \sa setContentHandler()*//*! \fn void QXmlReader::setErrorHandler( QXmlErrorHandler* handler ) Sets the error handler to \a handler. \sa errorHandler()*//*! \fn QXmlErrorHandler* QXmlReader::errorHandler() const Returns the error handler or 0 if none was set \sa setErrorHandler()*//*! \fn void QXmlReader::setLexicalHandler( QXmlLexicalHandler* handler ) Sets the lexical handler to \a handler. \sa lexicalHandler()*//*! \fn QXmlLexicalHandler* QXmlReader::lexicalHandler() const Returns the lexical handler or 0 if none was set. \sa setLexicalHandler()*//*! \fn void QXmlReader::setDeclHandler( QXmlDeclHandler* handler ) Sets the declaration handler to \a handler. \sa declHandler()*//*! \fn QXmlDeclHandler* QXmlReader::declHandler() const Returns the declaration handler or 0 if none was set. \sa setDeclHandler()*//*! \fn bool QXmlReader::parse( const QXmlInputSource& input ) Parses the XML document \a input. Returns TRUE if the parsing was successful, otherwise FALSE.*//*! \fn bool QXmlReader::parse( const QString& systemId ) Parses the XML document at the location \a systemId. Returns TRUE if the parsing was successful, otherwise FALSE.*//*! \class QXmlSimpleReader qxml.h \brief The QXmlSimpleReader class provides an implementation of a simple XML reader (i.e. parser). \module XML This XML reader is sufficient for simple parsing tasks. Here is a short list of the properties of this reader: <ul> <li> well-formed parser <li> does not parse any external entities <li> can do namespace processing </ul> For getting started see also the <a href="xml-sax.html#quickStart">Quick start</a>.*///guaranteed not to be a characaterconst QChar QXmlSimpleReader::QEOF = QChar((ushort)0xffff);/*! Constructs a simple XML reader.*/QXmlSimpleReader::QXmlSimpleReader(){ d = new QXmlSimpleReaderPrivate(); d->locator = new QXmlLocator( this ); entityRes = 0; dtdHnd = 0; contentHnd = 0; errorHnd = 0; lexicalHnd = 0; declHnd = 0; // default feature settings d->useNamespaces = TRUE; d->useNamespacePrefixes = FALSE; d->reportWhitespaceCharData = TRUE;}/*! Destroys a simple XML reader.*/QXmlSimpleReader::~QXmlSimpleReader(){ delete d->locator; delete d;}/*! Gets the state of a feature. \sa setFeature() hasFeature()*/bool QXmlSimpleReader::feature( const QString& name, bool *ok ) const{ if ( ok != 0 ) *ok = TRUE; if ( name == "http://xml.org/sax/features/namespaces" ) { return d->useNamespaces; } else if ( name == "http://xml.org/sax/features/namespace-prefixes" ) { return d->useNamespacePrefixes; } else if ( name == "http://trolltech.com/xml/features/report-whitespace-only-CharData" ) { return d->reportWhitespaceCharData; } else { qWarning( "Unknown feature " + name ); if ( ok != 0 ) *ok = FALSE; } return FALSE;}/*! Sets the state of a feature. Supported features are: <ul> <li> http://xml.org/sax/features/namespaces: if this feature is TRUE, namespace processing is performed <li> http://xml.org/sax/features/namespace-prefixes: if this feature is TRUE, the the original prefixed names and attributes used for namespace declarations are reported <li> http://trolltech.com/xml/features/report-whitespace-only-CharData: if this feature is TRUE, CharData that consists only of whitespace (and no other characters) is not reported via QXmlContentHandler::characters() </ul> \sa feature() hasFeature()*/void QXmlSimpleReader::setFeature( const QString& name, bool value ){ if ( name == "http://xml.org/sax/features/namespaces" ) { d->useNamespaces = value; } else if ( name == "http://xml.org/sax/features/namespace-prefixes" ) { d->useNamespacePrefixes = value; } else if ( name == "http://trolltech.com/xml/features/report-whitespace-only-CharData" ) { d->reportWhitespaceCharData = value; } else { qWarning( "Unknown feature " + name ); }}/*! Returns TRUE if the class has a feature named \a feature, otherwise FALSE. \sa setFeature() feature()*/bool QXmlSimpleReader::hasFeature( const QString& name ) const{ if ( name == "http://xml.org/sax/features/namespaces" || name == "http://xml.org/sax/features/namespace-prefixes" || name == "http://trolltech.com/xml/features/report-whitespace-only-CharData" ) { return TRUE; } else { return FALSE; }}/*! Returns 0 since this class does not support any properties.*/void* QXmlSimpleReader::property( const QString&, bool *ok ) const{ if ( ok != 0 ) *ok = FALSE; return 0;}/*! Does nothing since this class does not support any properties.*/void QXmlSimpleReader::setProperty( const QString&, void* ){}/*! Returns FALSE since this class does not support any properties.*/bool QXmlSimpleReader::hasProperty( const QString& ) const{ return FALSE;}/*! \reimp */void QXmlSimpleReader::setEntityResolver( QXmlEntityResolver* handler ){ entityRes = handler; }/*! \reimp */QXmlEntityResolver* QXmlSimpleReader::entityResolver() const{ return entityRes; }/*! \reimp */void QXmlSimpleReader::setDTDHandler( QXmlDTDHandler* handler ){ dtdHnd = handler; }/*! \reimp */QXmlDTDHandler* QXmlSimpleReader::DTDHandler() const{ return dtdHnd; }/*! \reimp */void QXmlSimpleReader::setContentHandler( QXmlContentHandler* handler ){ contentHnd = handler; }/*! \reimp */QXmlContentHandler* QXmlSimpleReader::contentHandler() const{ return contentHnd; }/*! \reimp */void QXmlSimpleReader::setErrorHandler( QXmlErrorHandler* handler ){ errorHnd = handler; }/*! \reimp */QXmlErrorHandler* QXmlSimpleReader::errorHandler() const{ return errorHnd; }/*! \reimp */void QXmlSimpleReader::setLexicalHandler( QXmlLexicalHandler* handler ){ lexicalHnd = handler; }/*! \reimp */QXmlLexicalHandler* QXmlSimpleReader::lexicalHandler() const{ return lexicalHnd; }/*! \reimp */void QXmlSimpleReader::setDeclHandler( QXmlDeclHandler* handler ){ declHnd = handler; }/*! \reimp */QXmlDeclHandler* QXmlSimpleReader::declHandler() const{ return declHnd; }/*! \reimp */bool QXmlSimpleReader::parse( const QXmlInputSource& input ){ init( input ); // call the handler if ( contentHnd ) { contentHnd->setDocumentLocator( d->locator ); if ( !contentHnd->startDocument() ) { d->error = contentHnd->errorString(); goto parseError; } } // parse prolog if ( !parseProlog() ) { d->error = XMLERR_ERRORPARSINGPROLOG; goto parseError; } // parse element if ( !parseElement() ) { d->error = XMLERR_ERRORPARSINGMAINELEMENT; goto parseError; } // parse Misc* while ( !atEnd() ) { if ( !parseMisc() ) { d->error = XMLERR_ERRORPARSINGMISC; goto parseError; } } // is stack empty? if ( !tags.isEmpty() ) { d->error = XMLERR_UNEXPECTEDEOF; goto parseError; } // call the handler if ( contentHnd ) { if ( !contentHnd->endDocument() ) { d->error = contentHnd->errorString(); goto parseError; } } return TRUE; // error handlingparseError: reportParseError(); tags.clear(); return FALSE;}/*! Parses the prolog [22].*/bool QXmlSimpleReader::parseProlog(){ bool xmldecl_possible = TRUE; bool doctype_read = FALSE; const signed char Init = 0; const signed char EatWS = 1; // eat white spaces const signed char Lt = 2; // '<' read const signed char Em = 3; // '!' read const signed char DocType = 4; // read doctype const signed char Comment = 5; // read comment const signed char PI = 6; // read PI const signed char Done = 7; const signed char InpWs = 0; const signed char InpLt = 1; // < const signed char InpQm = 2; // ? const signed char InpEm = 3; // ! const signed char InpD = 4; // D const signed char InpDash = 5; // - const signed char InpUnknown = 6; // use some kind of state machine for parsing static signed char table[7][7] = { /* InpWs InpLt InpQm InpEm InpD InpDash InpUnknown */ { EatWS, Lt, -1, -1, -1, -1, -1 }, // Init { -1, Lt, -1, -1, -1, -1, -1 }, // EatWS { -1, -1, PI, Em, Done, -1, Done }, // Lt { -1, -1, -1, -1, DocType, Comment, -1 }, // Em { EatWS, Lt, -1, -1, -1, -1, -1 }, // DocType { EatWS, Lt, -1, -1, -1, -1, -1 }, // Comment { EatWS, Lt, -1, -1, -1, -1, -1 } // PI }; signed char state = Init; signed char input; bool parseOk = TRUE; while ( TRUE ) { // read input if ( atEnd() ) { d->error = XMLERR_UNEXPECTEDEOF; goto parseError; } if ( is_S(c) ) { input = InpWs; } else if ( c == '<' ) { input = InpLt; } else if ( c == '?' ) { input = InpQm; } else if ( c == '!' ) { input = InpEm; } else if ( c == 'D' ) { input = InpD; } else if ( c == '-' ) { input = InpDash; } else { input = InpUnknown; } // get new state state = table[state][input]; // in some cases do special actions depending on state switch ( state ) { case EatWS: // XML declaration only on first position possible xmldecl_possible = FALSE; // eat white spaces eat_ws(); break; case Lt: // next character next(); break; case Em: // XML declaration only on first position possible xmldecl_possible = FALSE; // next character next(); break; case DocType: parseOk = parseDoctype(); break; case Comment
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -