📄 qxml.cpp
字号:
}/*! Returns an enumeration of all prefixes currently declared. Note: if there is a default prefix, it will not be returned in this enumeration; check for the default prefix using uri() with an argument of "".*/QStringList QXmlNamespaceSupport::prefixes() const{ QStringList list; QMap<QString, QString>::ConstIterator itc, it = ns.begin(); while ( (itc=it) != ns.end() ) { ++it; if ( !itc.key().isEmpty() ) list.append( itc.key() ); } return list;}/*! Returns a list of all prefixes currently declared for a URI. The xml: prefix will be included. If you want only one prefix that's mapped to the namespace URI, and you don't care which one you get, use the prefix() function instead. Note: the empty (default) prefix is never included in this enumeration; to check for the presence of a default namespace, use uri() with an argument of "".*/QStringList QXmlNamespaceSupport::prefixes( const QString& uri ) const{ QStringList list; QMap<QString, QString>::ConstIterator itc, it = ns.begin(); while ( (itc=it) != ns.end() ) { ++it; if ( itc.data() == uri && !itc.key().isEmpty() ) list.append( itc.key() ); } return list;}/*! Starts a new namespace context. Normally, you should push a new context at the beginning of each XML element: the new context will automatically inherit the declarations of its parent context, but it will also keep track of which declarations were made within this context.*/void QXmlNamespaceSupport::pushContext(){ nsStack.push( ns );}/*! Reverts to the previous namespace context. Normally, you should pop the context at the end of each XML element. After popping the context, all namespace prefix mappings that were previously in force are restored.*/void QXmlNamespaceSupport::popContext(){ if( !nsStack.isEmpty() ) ns = nsStack.pop();}/*! Resets this namespace support object for reuse.*/void QXmlNamespaceSupport::reset(){ nsStack.clear(); ns.clear(); ns.insert( "xml", "http://www.w3.org/XML/1998/namespace" ); // the XML namespace}/********************************************* * * QXmlAttributes * *********************************************//*! \class QXmlAttributes qxml.h \brief The QXmlAttributes class provides XML attributes. \module XML If attributes are reported by QXmlContentHandler::startElement() this class is used to pass the attribute values. It provides you with different functions to access the attribute names and values.*//*! \fn QXmlAttributes::QXmlAttributes() Constructs an empty attribute list.*//*! \fn QXmlAttributes::~QXmlAttributes() Destructs attributes.*//*! Look up the index of an attribute by an XML 1.0 qualified name. Returns the index of the attribute (starting with 0) or -1 if it wasn't found. See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/int QXmlAttributes::index( const QString& qName ) const{ return qnameList.findIndex( qName );}/*! Looks up the index of an attribute by a namespace name. \a uri specifies the namespace URI, or the empty string if the name has no namespace URI. \a localPart specifies the attribute's local name. Returns the index of the attribute (starting with 0) or -1 if it wasn't found. See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/int QXmlAttributes::index( const QString& uri, const QString& localPart ) const{ uint count = uriList.count(); for ( uint i=0; i<count; i++ ) { if ( uriList[i] == uri && localnameList[i] == localPart ) return i; } return -1;}/*! Returns the number of attributes in the list.*/int QXmlAttributes::length() const{ return valueList.count();}/*! Looks up an attribute's local name by index (starting with 0). See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/QString QXmlAttributes::localName( int index ) const{ return localnameList[index];}/*! Looks up an attribute's XML 1.0 qualified name by index (starting with 0). See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/QString QXmlAttributes::qName( int index ) const{ return qnameList[index];}/*! Looks up an attribute's namespace URI by index (starting with 0). See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/QString QXmlAttributes::uri( int index ) const{ return uriList[index];}/*! Looks up an attribute's type by index (starting with 0). At the moment only 'CDATA' is returned.*/QString QXmlAttributes::type( int ) const{ return "CDATA";}/*! Looks up an attribute's type by XML 1.0 qualified name. At the moment only 'CDATA' is returned.*/QString QXmlAttributes::type( const QString& ) const{ return "CDATA";}/*! Looks up an attribute's type by namespace name. The first parameter specifies the namespace URI, or the empty string if the name has no namespace URI. The second parameter specifies the attribute's local name. At the moment only 'CDATA' is returned.*/QString QXmlAttributes::type( const QString&, const QString& ) const{ return "CDATA";}/*! Looks up an attribute's value by index (starting with 0).*/QString QXmlAttributes::value( int index ) const{ return valueList[index];}/*! Looks up an attribute's value by XML 1.0 qualified name. See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/QString QXmlAttributes::value( const QString& qName ) const{ int i = index( qName ); if ( i == -1 ) return QString::null; return valueList[ i ];}/*! Looks up an attribute's value by namespace name. \a uri specifies the namespace URI, or the empty string if the name has no namespace URI. \a localName specifies the attribute's local name. See also the <a href="xml-sax.html#namespaces">namespace description</a>.*/QString QXmlAttributes::value( const QString& uri, const QString& localName ) const{ int i = index( uri, localName ); if ( i == -1 ) return QString::null; return valueList[ i ];}/********************************************* * * QXmlInputSource * *********************************************//*! \class QXmlInputSource qxml.h \brief The QXmlInputSource class is the source where XML data is read from. \module XML All subclasses of QXmlReader read the input from this class.*//*! Returns all the data this input source contains.*/const QString& QXmlInputSource::data() const{ return input;}/*! Constructs a input source which contains no data.*/QXmlInputSource::QXmlInputSource( ){ input = "";}/*! Constructs a input source and get the data from the text stream.*/QXmlInputSource::QXmlInputSource( QTextStream& stream ){ QByteArray rawData; if ( stream.device()->isDirectAccess() ) { rawData = stream.device()->readAll(); } else { int nread = 0; const int bufsize = 512; while ( !stream.device()->atEnd() ) { rawData.resize( nread + bufsize ); nread += stream.device()->readBlock( rawData.data()+nread, bufsize ); } rawData.resize( nread ); } readInput( rawData );}/*! Constructs a input source and get the data from a file. If the file cannot be read the input source is empty.*/QXmlInputSource::QXmlInputSource( QFile& file ){ if ( !file.open(IO_ReadOnly) ) { input = ""; return; } QByteArray rawData = file.readAll(); readInput( rawData ); file.close();}/*! Destructor.*/QXmlInputSource::~QXmlInputSource(){}/*! Sets the data of the input source to \a dat.*/void QXmlInputSource::setData( const QString& dat ){ input = dat;}/*! Read the XML file from the byte array; try to recoginize the encoding.*/// ### The input source should not do the encoding detection!void QXmlInputSource::readInput( QByteArray& rawData ){ QBuffer buf( rawData ); buf.open( IO_ReadOnly ); QTextStream *stream = new QTextStream( &buf ); QChar tmp; // assume UTF8 or UTF16 at first stream->setEncoding( QTextStream::UnicodeUTF8 ); input = ""; // read the first 5 characters for ( int i=0; i<5; i++ ) { *stream >> tmp; input += tmp; } // starts the document with an XML declaration? if ( input == "<?xml" ) { // read the whole XML declaration do { *stream >> tmp; input += tmp; } while( tmp != '>' ); // and try to find out if there is an encoding int pos = input.find( "encoding" ); if ( pos != -1 ) { QString encoding; do { pos++; if ( pos > (int)input.length() ) goto finished; } while( input[pos] != '"' && input[pos] != '\'' ); pos++; while( input[pos] != '"' && input[pos] != '\'' ) { encoding += input[pos]; pos++; if ( pos > (int)input.length() ) goto finished; } delete stream; stream = new QTextStream( &buf ); stream->setCodec( QTextCodec::codecForName( encoding ) ); buf.reset(); input = ""; } }finished: input += stream->read(); delete stream; buf.close();}/********************************************* * * QXmlDefaultHandler * *********************************************//*! \class QXmlContentHandler qxml.h \brief The QXmlContentHandler class provides an interface to report logical content of XML data. \module XML If the application needs to be informed of basic parsing events, it implements this interface and sets it with QXmlReader::setContentHandler(). The reader reports basic document-related events like the start and end of elements and character data through this interface. The order of events in this interface is very important, and mirrors the order of information in the document itself. For example, all of an element's content (character data, processing instructions, and/or subelements) will appear, in order, between the startElement() event and the corresponding endElement() event. The class QXmlDefaultHandler gives a default implementation for this interface; subclassing from this class is very convenient if you want only be informed of some parsing events. See also the <a href="xml.html#introSAX2">Introduction to SAX2</a>. \sa QXmlDTDHandler QXmlDeclHandler QXmlEntityResolver QXmlErrorHandler QXmlLexicalHandler*//*! \fn void QXmlContentHandler::setDocumentLocator( QXmlLocator* locator ) The reader calls this function before he starts parsing the document. The argument \a locator is a pointer to a QXmlLocator which allows the application to get the actual position of the parsing in the document. Do not destroy the \a locator; it is destroyed when the reader is destroyed (do not use the \a locator after the reader got destroyed).*//*! \fn bool QXmlContentHandler::startDocument() The reader calls this function when he starts parsing the document. The reader will call this function only once before any other functions in this class or in the QXmlDTDHandler class are called (except QXmlContentHandler::setDocumentLocator()). If this function returns FALSE the reader will stop parsing and will report
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -