PDA

View Full Version : XML Parsing in Qt 3.3



ToddAtWSU
13th April 2007, 21:31
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):


QDir xmlDir = QDir( xmlDirString, QString( "*.xml" ) );
InterceptReader intReader;
QXmlSimpleReader reader;
reader.setContentHandler( &intReader );
read.setErrorHandler( &intReader );
int numFiles = xmlDir.count( );
for( int i = 0 ; i < numFiles ; i++ )
{
QFile file( xmlDir[i] );
reader.parse( &file );
char *ref;
char *diff;
intReader.getEventIds( ref, diff ); // These char*s are malloced using strdup
if( ref && diff )
{
fprintf( stderr, "ref = %s, diff = %s\n", ref, diff );
free( ref );
ref = NULL;
free( diff );
diff = NULL;
}
else if( ref && !diff )
{
fprintf( stderr, "ref = %s\n", ref );
free( ref );
ref = NULL;
}
else if( !ref && diff )
{
fprintf( stderr, "diff = %s\n", diff );
free( diff );
diff = NULL;
}
}

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!

marcel
14th April 2007, 07:38
Could you show us your InterceptReader class? At least the error handling routines...

ToddAtWSU
18th April 2007, 13:42
bool InterceptReader::fatalError( const QXmlParseException &exception )
{
qWarning( "Line %d, column %d: %s", exception.lineNumber( ),
exception.columnNumber( ), exception.message( ).ascii( ) );
return false;
}


bool InterceptReader::startElement( const QString &namespaceURI,
const QString &localName,
const QString &qName,
const QXmlAttributes &attribs )
{
fprintf( stderr, "start element = %s\n", qName.ascii( ) );
if( qName == QString( "ref_event" ) )
{
mOpenElement = qName;
}
else if( qName == QString( "dif_event" ) )
{
mOpenElement = qName;
}
return true;
}


bool InterceptReader::characters( const QString &str )
{
if( mOpenElement == QString( "ref_event" ) )
{
mRefEventId= str;
}
else if( mOpenElement == QString( "dif_event" ) )
{
mDiffEventId= str;
}
return true;
}


bool InterceptReader::endElement( const QString &namespaceURI,
const QString &localName,
const QString &qName )
{
fprintf( stderr, "end element = %s\n", qName.ascii( ) );
if( qName == QString( "ref_event" ) )
{
mOpenElement = QString( "" );
}
else if( qName == QString( "dif_event" ) )
{
mOpenElement = QString( "" );
}
return true;
}

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!

vermarajeev
18th April 2007, 14:07
what is reader.parse( &file ) value?

ToddAtWSU
18th April 2007, 16:34
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

reader.parse( &file )
QXmlSimpleReader::parseBeginOrContinue
QXmlSimpleReader::parseProlog
QXmlSimpleReader::unexpectedEof
QXmlSimpleReader::reportParseError
InterceptReader::fatalError

Thanks again!

ToddAtWSU
18th April 2007, 18:54
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!