Results 1 to 6 of 6

Thread: XML Parsing in Qt 3.3

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face XML Parsing in Qt 3.3

    I am trying to parse through a group of XML files in Qt 3.3.7 following the Blanchette and Summerfield guide book on how to parse XML files using SAX. I have created a class called InterceptReader which looks identical to their SaxHandler class except instead of making a QListView I just have a couple QStrings to store information during the parsing and an additional function called getIds( char*, char* ) which creates 2 char*s and uses strdup( ) to allocate and create these char*s so I can use them outside the class. The two char *s are the pieces of data I am concerned about when parsing through the XML file and which is stored in QStrings.

    I then in my calling class have a function called openXMLFiles( ) which looks like this (sorry for any misspellings, my code is on a stand-alone machine with no network connectivity):

    Qt Code:
    1. QDir xmlDir = QDir( xmlDirString, QString( "*.xml" ) );
    2. InterceptReader intReader;
    3. reader.setContentHandler( &intReader );
    4. read.setErrorHandler( &intReader );
    5. int numFiles = xmlDir.count( );
    6. for( int i = 0 ; i < numFiles ; i++ )
    7. {
    8. QFile file( xmlDir[i] );
    9. reader.parse( &file );
    10. char *ref;
    11. char *diff;
    12. intReader.getEventIds( ref, diff ); // These char*s are malloced using strdup
    13. if( ref && diff )
    14. {
    15. fprintf( stderr, "ref = %s, diff = %s\n", ref, diff );
    16. free( ref );
    17. ref = NULL;
    18. free( diff );
    19. diff = NULL;
    20. }
    21. else if( ref && !diff )
    22. {
    23. fprintf( stderr, "ref = %s\n", ref );
    24. free( ref );
    25. ref = NULL;
    26. }
    27. else if( !ref && diff )
    28. {
    29. fprintf( stderr, "diff = %s\n", diff );
    30. free( diff );
    31. diff = NULL;
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    I plan to do more with this code later but wanted to make sure I was parsing it correctly. But for ever file I print out an error that says "Line 1, column 1: unexpected end of file". I have opened the XML files and they are filled out as well as being used by other programs. And I am basically using the SAX reader in the Qt 3 book verbatim as of right now. Any ideas why I would be getting this end of file error on a valid XML file? I am relatively new to XML but thought parsing through the file should be simple. Thanks for your help!

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML Parsing in Qt 3.3

    Could you show us your InterceptReader class? At least the error handling routines...

  3. #3
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Re: XML Parsing in Qt 3.3

    Qt Code:
    1. bool InterceptReader::fatalError( const QXmlParseException &exception )
    2. {
    3. qWarning( "Line %d, column %d: %s", exception.lineNumber( ),
    4. exception.columnNumber( ), exception.message( ).ascii( ) );
    5. return false;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool InterceptReader::startElement( const QString &namespaceURI,
    2. const QString &localName,
    3. const QString &qName,
    4. const QXmlAttributes &attribs )
    5. {
    6. fprintf( stderr, "start element = %s\n", qName.ascii( ) );
    7. if( qName == QString( "ref_event" ) )
    8. {
    9. mOpenElement = qName;
    10. }
    11. else if( qName == QString( "dif_event" ) )
    12. {
    13. mOpenElement = qName;
    14. }
    15. return true;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool InterceptReader::characters( const QString &str )
    2. {
    3. if( mOpenElement == QString( "ref_event" ) )
    4. {
    5. mRefEventId= str;
    6. }
    7. else if( mOpenElement == QString( "dif_event" ) )
    8. {
    9. mDiffEventId= str;
    10. }
    11. return true;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. bool InterceptReader::endElement( const QString &namespaceURI,
    2. const QString &localName,
    3. const QString &qName )
    4. {
    5. fprintf( stderr, "end element = %s\n", qName.ascii( ) );
    6. if( qName == QString( "ref_event" ) )
    7. {
    8. mOpenElement = QString( "" );
    9. }
    10. else if( qName == QString( "dif_event" ) )
    11. {
    12. mOpenElement = QString( "" );
    13. }
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 

    Like I said this is more of a test right now to make sure I can parse through the XML and grab certain items and later I will implement more of what I want to do with them. But this is my basic XML parser. Thanks for your continued help!

  4. #4
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: XML Parsing in Qt 3.3

    what is reader.parse( &file ) value?

  5. #5
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Re: XML Parsing in Qt 3.3

    What do you mean by
    what is reader.parse( &file ) value?
    It returns false if that is what you mean?

    I have run my code through DBX and my stack's last 6 lines looks like
    Qt Code:
    1. reader.parse( &file )
    2. QXmlSimpleReader::parseBeginOrContinue
    3. QXmlSimpleReader::parseProlog
    4. QXmlSimpleReader::unexpectedEof
    5. QXmlSimpleReader::reportParseError
    6. InterceptReader::fatalError
    To copy to clipboard, switch view to plain text mode 

    Thanks again!

  6. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Wink [SOLVED] Re: XML Parsing in Qt 3.3

    I found my problem at least as to why I got my error. My XMLs were in a different directory than my executable and when getting a QDir, using the [] operator only returned the filename and when I created the QFile I needed to make sure to include the actual path to the files when giving the filename to create the QFile. Now that I give the path/filename the files don't error out. Thanks for your help anyways!

Similar Threads

  1. Parsing XML file
    By Djony in forum Qt Programming
    Replies: 7
    Last Post: 8th January 2007, 15:03
  2. parsing QtableWidget from a QStackedWidget
    By rosmarcha in forum Qt Programming
    Replies: 10
    Last Post: 13th October 2006, 14:36
  3. Config file parsing
    By Vash5556 in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2006, 23:11
  4. HTML Parsing
    By awalesminfo in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2006, 11:31
  5. Trouble parsing using simple QRegExp
    By johnny_sparx in forum Qt Programming
    Replies: 4
    Last Post: 24th February 2006, 00:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.