- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
FILES, STREAMS, AND XML in Font
CHAPTER 8 FILES, STREAMS, AND XML Data Matrix ECC200 Maker In None Using Barcode generator for Font Control to generate, create ECC200 image in Font applications. www.OnBarcode.comPrinting UCC.EAN - 128 In None Using Barcode maker for Font Control to generate, create EAN / UCC - 14 image in Font applications. www.OnBarcode.comdocumentElement.insertBefore( a, QDomNode() ); } else if( elements.size() == 1 ) { QDomElement a = elements.at(0).toElement(); QDomElement r = document.createElement( "revision" ); r.setAttribute( "count", QString::number( a.elementsByTagName( "revision" ).size() + 1 ) ); a.appendChild( r ); } UCC - 12 Generation In None Using Barcode generation for Font Control to generate, create UPCA image in Font applications. www.OnBarcode.comDrawing Barcode In None Using Barcode encoder for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comReading XML Files with SAX
Barcode Creator In None Using Barcode printer for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comMake ECC200 In None Using Barcode creation for Font Control to generate, create Data Matrix 2d barcode image in Font applications. www.OnBarcode.comThe simple API for XML (SAX) can be used only to read XML files. It works by reading the file and locating opening tags, closing tags, attributes, and text; and calling functions in the handler objects set up to handle the different parts of an XML document. The benefit of this approach compared with using a DOM document is that the entire file does not have to be loaded into memory at once. To use SAX, three classes are used: QXmlInputSource, QXmlSimpleReader, and a handler. Listing 8-16 shows the main function of an application using SAX to parse a file. The QXmlInputSource is used to provide a predefined interface between the QFile and the QXmlSimpleReader object. The QXmlSimpleReader is a specialized version of the QXmlReader class. The simple reader is powerful enough to be used in almost all cases. The reader has a content handler that is assigned using the setContentHandler method. The content handler must inherit the QXmlContentHandler, and that is exactly what the MyHandler class does. Having set everything up, it is just a matter of calling the parse(const QXmlInputSource *, bool) method, passing the XML input source object as a parameter, and waiting for the reader to report everything worth knowing to the handler. Listing 8-16. Setting up a SAX reader with a custom handler class int main( int argc, char **argv ) { QFile file( "simple.xml" ); if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) ) { qDebug( "Failed to open file for reading." ); return -1; } QXmlInputSource source( &file ); MyHandler handler; Making PDF 417 In None Using Barcode drawer for Font Control to generate, create PDF 417 image in Font applications. www.OnBarcode.comPrint Postnet 3 Of 5 In None Using Barcode creator for Font Control to generate, create Delivery Point Barcode (DPBC) image in Font applications. www.OnBarcode.comCHAPTER 8 FILES, STREAMS, AND XML
Generate Data Matrix In None Using Barcode generation for Word Control to generate, create ECC200 image in Office Word applications. www.OnBarcode.comPainting Data Matrix ECC200 In Java Using Barcode creation for Java Control to generate, create Data Matrix ECC200 image in Java applications. www.OnBarcode.comQXmlSimpleReader reader; reader.setContentHandler( &handler ); reader.parse( source ); file.close(); return 0; } The declaration of the handler class MyHandler can be seen in Listing 8-17. The class inherits from QXmlDefaultHandler, which is derived from QXmlContentHandler. The benefit of inheriting QXmlDefaultHandler is that the default handler class provides dummy implementations of all the methods that you otherwise would have had to implement as stubs. The methods in the handler class get called by the reader when something is encountered. You want to handle text and tags and know when the parsing process starts and ends, so the methods shown in the class declaration have been implemented. All methods return a bool value, which is used to stop the parsing if an error is encountered. All methods must return true for the reader to continue reading. Listing 8-17. The MyHandler SAX handler class class MyHandler : public QXmlDefaultHandler { public: bool startDocument(); bool endDocument(); bool startElement( const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts ); bool endElement( const QString &namespaceURI, const QString &localName, const QString &qName ); bool characters( const QString &ch ); }; All methods except startElement look more or less like the method shown in Listing 8-18. A simple text is printed to the debug console, and then true is returned. In the case of endElement (shown in the listing), an argument is printed as well. Listing 8-18. A simple handling class method bool MyHandler::endElement( const QString &namespaceURI, const QString &localName, const QString &qName ) { qDebug() << "End of element" << qName; return true; } Scanning Barcode In None Using Barcode decoder for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comDrawing Barcode In None Using Barcode printer for Word Control to generate, create Barcode image in Microsoft Word applications. www.OnBarcode.comCHAPTER 8 FILES, STREAMS, AND XML
Barcode Drawer In Java Using Barcode drawer for Eclipse BIRT Control to generate, create Barcode image in Eclipse BIRT applications. www.OnBarcode.comPainting UPC-A Supplement 5 In Java Using Barcode maker for Java Control to generate, create Universal Product Code version A image in Java applications. www.OnBarcode.comThe startElement method, shown in Listing 8-19, is slightly more complex. First, the element s name is printed; then the list of attributes passed through an QXmlAttributes object is printed. The QXmlAttributes is not a standard container, so you must iterate through it using an index variable instead of just using the foreach macro. Before the method ends, you return true to tell the reader that everything is working as expected. Listing 8-19. The startElement method lists the attributes of the element. bool MyHandler::startElement( const QString &namespaceURI, const QString &localName, const QString &qName, const QXmlAttributes &atts ) { qDebug() << "Start of element" << qName; for( int i=0; i<atts.length(); ++i ) qDebug() << " " << atts.qName(i) << "=" << atts.value(i); return true; } The reason for printing the qName instead of the namespaceURI or localName is that the qName is the tag name that you expect. Namespaces and local names are beyond the scope of this book. It is not very complicated to build an XML parser by implementing a SAX handler. As soon as you want to convert the XML data into custom data for your application, you should consider using SAX. Because the entire document is not loaded at once, the memory requirements of the application are reduced, which might mean that your application runs more quickly. Quick Response Code Encoder In Java Using Barcode drawer for Android Control to generate, create QR Code JIS X 0510 image in Android applications. www.OnBarcode.comEAN128 Printer In None Using Barcode maker for Online Control to generate, create EAN128 image in Online applications. www.OnBarcode.comMake QR Code 2d Barcode In Visual Studio .NET Using Barcode creation for ASP.NET Control to generate, create QR Code ISO/IEC18004 image in ASP.NET applications. www.OnBarcode.comBarcode Creator In Objective-C Using Barcode generator for iPad Control to generate, create Barcode image in iPad applications. www.OnBarcode.comPrint Barcode In .NET Using Barcode maker for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comBarcode Generator In Objective-C Using Barcode generator for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.com |
|