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!
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!