c++ - Qt Read XML File -
i trying read xml file in qt, generated using different method. here xml file:
<?xml version="1.0" encoding="utf-8"?> <project> <editortheme>null</editortheme> <modules> <module> <name>module_renderer</name> <position>471,164</position> <size>200,100</size> <locked>true</locked> <visible>true</visible> </module> <module> <name>module_console</name> <position>200,229</position> <size>256,192</size> <locked>true</locked> <visible>false</visible> </module> <module> <name>module_resourcetoolkit</name> <position>1049,328</position> <size>200,100</size> <locked>true</locked> <visible>true</visible> </module> <module> <name>module_celleditor</name> <position>542,564</position> <size>200,100</size> <locked>true</locked> <visible>false</visible> </module> </modules> </project>
and here code using parse file:
project projectloader::loadlastproject( consolemodule* console ) { project project; // load xml qfile file( "c:/users/krynn/desktop/lastproject.xml" ); if( !file.open( qfile::readonly | qfile::text ) ) { // print error cannot open } qxmlstreamreader reader; console->outputdisplay->append( "test" ); reader.setdevice( &file ); reader.readnext(); while( !reader.atend() && !reader.haserror() ) { reader.readnext(); if( reader.isstartelement() ) { qstring name = reader.name().tostring(); if( reader.name() == "project" ) { reader.readnextstartelement(); if( reader.name().tostring() == "editortheme" ) { // append project theme console->outputdisplay->append( "theme detected: " + reader.name().tostring() + " " + reader.readelementtext() ); } reader.readnextstartelement(); if( reader.name().tostring() == "modules" ) { // how proceed?? console->outputdisplay->append( qstring( "" ) + " " + reader.name().tostring() + " " + reader.readelementtext() ); } } } } if( reader.haserror() ) { console->outputdisplay->append( "xml error: " + reader.errorstring() ); } else if( reader.atend() ) { console->outputdisplay->append( "end of xml file reached" ); } file.close(); return project; }
and here visual output code gives me:
really, don't know how go loading module data within xml file. using plain text file store stuff, want upgrade. appreciated.
nevermind figured out.
project projectloader::loadlastproject( consolemodule* console ) { project project; // load xml qfile file( "c:/users/krynn/desktop/lastproject.xml" ); if( !file.open( qfile::readonly | qfile::text ) ) { // print error cannot open } qxmlstreamreader reader; reader.setdevice( &file ); reader.readnext(); int count = 0; while( !reader.atend() ) { //&& !reader.haserror() reader.readnext(); if( reader.isstartelement() ) { if( reader.name().tostring() == "module" ) { windowmodulesavedata data; reader.readnextstartelement(); data.name = reader.readelementtext(); // name reader.readnextstartelement(); data.position = convertstringtoqpoint( reader.readelementtext() ); console->outputdisplay->append( convertqpointtostring(data.position) ); reader.readnextstartelement(); data.size = convertstringtoqsize( reader.readelementtext() ); reader.readnextstartelement(); data.islocked = reader.readelementtext() == "true" ? true : false; reader.readnextstartelement(); data.isvisible = reader.readelementtext() == "true" ? true : false; project.modules.push_back( data ); console->outputdisplay->append("loaded module"); } count++; } } console->outputdisplay->append( qstring::number( count ) ); if( reader.haserror() ) { console->outputdisplay->append( "xml error: " + reader.errorstring() ); } else if( reader.atend() ) { console->outputdisplay->append( "end of xml file reached" ); } file.close(); return project; }
the above code may error prone, because assumes next child may instead of testing it. enough though.
Comments
Post a Comment